Hi everyone,
I am trying to build an RSVP page. What I have built is a name input box and a button titled “find my invitation.” When that button is clicked, I want it to search through my collection database called “GuestList” and if the name is found, then open the collapsed container box with the names of everyone in that family and selection tags of “Joyfully Accept” and “Regretfully Decline” next to each name. (in my guest list collection I have a field identifying who is a part of what family).
I have also created a “Responses” collection to store everyone’s responses. I can’t seem to get my code to work. It’s not finding anyone’s name. Any ideas on what I’m doing wrong?
import wixData from ‘wix-data’;
var number = 0;
var family = " ";
$w.onReady(function () {
$w(“#box1”).collapse();
$w(“#errortext”).hide();
$w(“#submittext”).hide();
});
export function button1_click(event) { //find my invitation button
$w(‘#box1’).collapse();
$w(‘#errortext’).hide();
for (var i = 1; i < 7; i++) {
var q = i;
var identifier = “#name” + q;
$w(identifier).collapse();
identifier = “#selectionTags” + q;
$w(identifier).collapse();
}
//$w(“#group1”).collapse();
var name = $w(‘#nameinput’).value;
const words = name.split(" ");
name = words[0][0].toUpperCase() + words[0].substr(1).toLowerCase() + " " + words[1][0].toUpperCase() + words[1].substr(1).toLowerCase();
$w(‘#nameinput’).value = name
wixData.query(“Responses”) //responsesdatabase
.contains(“name”, name)
.find()
.then((res)=> {
if(res.items.length == 1) {
$w(“#errortext”).text = “You or someone in your party has already submitted an RSVP. Thank you!”;
$w(“#errortext”).show();
}
else {
wixData.query(“GuestList”) //check for firstname matches
.eq(“fullName”, name)
.find()
.then( (res) => {
if(res.items.length == 1) {
let firstItem = res.items[0];//First item in results
$w(‘#box1’).expand();
//$w(“#submittext”).hide();
number = firstItem.numberOfGuests;
family = firstItem.family; //family name identifies party
if (number == 1) { //if only one guest
$w(‘#text2’).text = “We have reserved one seat in your honour.”
$w(‘#name1’).expand();
$w(‘#name1’).text = name;
$w(‘#selectionTags1’).expand();
}
else { //if multiple guests
$w(‘#text2’).text = “We have reserved " + number + " seats in your honour.”
wixData.query(“GuestList”) //find # of people w family name
.eq(“family”, family)
.find()
.then( (res) => {
//$w(“#submittext”).hide();
//fill in each name in rsvp section
for (let k = 0; k < number; k++) {
var q = k + 1;
var identifier = “#selectionTags” + q;
$w(identifier).expand();
var identifier = “#name” + q;
$w(identifier).text = res.items[k].fullName;
$w(identifier).expand();
}
} );
}
} else {
$w(‘#nameinput’).value = “Your Name Was Not Found”;
$w(‘#errortext’).text = “Please try again, or contact the couple for help.”;
$w(‘#errortext’).show();
}
} );
}
} );
}
export function button2_click(event) {
$w(“#button2”).disable();
//$w(“#group1”).expand();
for (var i = 0; i < number; i++) {
//add to database
var q = i + 1;
var identifier = “#name” + q;
var name = $w(identifier).text;
let tag = “#selectionTags” + q;
if ($w(tag).selectedIndices[0] == 0) {//accept
//add to database
let toInsert = {
“name”: name,
“rsvp”: “Yes”
};
wixData.insert(“Responses”, toInsert)
.then( (results) => {
$w(“#submittext”).show();
} )
.catch( (err) => {
let errorMsg = err;
} );
}
else if ($w(tag).selectedIndices[0] == 1){ //decline
let toInsert = {
“name”: name,
“rsvp”: “No”
};
wixData.insert(“Responses”, toInsert)
.then( (results) => {
$w(“#submittext”).show();
} )
.catch( (err) => {
let errorMsg = err;
} );
}
}
}