Comparing 2 Datasets to bring them to another page

Hello, I am hoping I can get help with something.

I have created a page that has input fields First Name and Last Name for my guests to enter. I also created a data collection titled “Wedding RSVP” that has the fields First and Last. I am trying to have guests enter their name, and once they press the “next” button, if the first and last name entered are correct (matches the first and last name in database), it will go to a dynamic page. Basically, I need it to go to a dynamic page if the 2 inputs are the same as the dataset.

I have tried using a code to make this happen, but I can’t seem to get it to work. Can anyone let me know where I am going wrong or help me out?

Thanks!


This is currently my code:

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
export function button1_click(event, $w) {
let input1 = $w(‘#input1’).value;
let input2 = $w(‘#input2’).value;
wixData.query(“WeddingRSVP”)
.eq(‘First’, input1)
.eq(‘Last’, input2)
.find()
.then((results) => {
if (results.items.length> 0) {
wixLocation.to(/copy-of-plus-ones/${results.items[0]._id});
}
});
}
});

  1. It can be an issue with upper-case vs lower-case (the query is case-sensitive).
  2. Maybe the collection permissions are not set correctly.

Add:

console.log(results.items);

to the .then() part and see what it logs.

What should the collection permissions be set to?

@jazzmin_d it depends on who should be able to read.
But according to your screenshot, you put the “import” line in the wrong place. It must be at the very top of your code (first line, before everything else. Event before the $w.onReady() )

@jonatandor35 I removed the code so that the import is at the top, but should that code go in another spot or can I remove it altogether?

I still cannot get it to work.

@jazzmin_d post your code

I see a few problems with your code:

Make sure that you need to wait for values of the text input fields to update . You will want to put a setTimeout() in your button1_click() event handler.

You need to make sure that your are using field keys of the database collection and not field names:


So, the filter needs to be like this:

.eq('first', input1)
.eq('last', input2)

Notice that the field keys start with lower case letters: not First, but first

I hope that helps

Thanks for all of your help so far.

I have made the necessary changes but still cannot get it to work.
Here is my code:

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

export function button1_click(event, $w) {
setTimeout(() => {
let input1 = $w(‘#input1’).value;
let input2 = $w(‘#input2’).value;
}, 10); // 10 milliseconds works for me
}
let input1 = $w(‘#input1’).value;
let input2 = $w(‘#input2’).value;

wixData.query(“dataset1”)
.eq(‘name’, input1)
.eq(‘lastName’, input2)
.find()
.then((results) => {
if (results.items.length> 0) {
wixLocation.to(/copy-of-plus-ones/${results.items[0]._id});
}
});

You have errors in your code:

  1. You need to have a white-space after the word “function”.
  2. It’s not clear why you need this timeout…
  3. It’s not clear why you need this timeout.
  4. You don’t call any function from the setTimeout(), so it doesn’t run the collection query then.
  5. You have an extra let input1 and extra let input2.

@jonatandor35 Hi J.D. I added that based on the suggestion of a user above.

I have removed it, so this is my current code…
My problem is that when I click the ‘next’ button, nothing is happening…

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

export function button1_click(event, $w) {
let input1 = $w(‘#input1’).value;
let input2 = $w(‘#input2’).value;
wixData.query(“dataset1”)
.eq(‘name’, input1)
.eq(‘lastName’, input2)
.find()
.then((results) => {
if (results.items.length > 0) {
wixLocation.to(/copy-of-plus-ones/${results.items[0]._id});
}
});
}

@jazzmin_d you need to use the name of your collection. Not “dataset1”.