Juanita:
Sorry I forgot you are a newbie to javascripting.
This line
.eq("email", userEmail) < -- -- -- -- -- -- - ensure unique use of email
Doesn’t work because it was me highlighting the changed test in your .eq(). If you remove everything to the right of the closing parenthesis (i.e. < – – – – – – - ensure unique use of email ) it should be fine
So the code should be:
.eq("email", userEmail)
The other thing I would do is handle the case where this test fails:
if (results.items.length === 0)
So you actually want to do something (at least for testing purposes if nothing else) on two other conditions. One condition is
} else if (results.items.length > 1) {
In this case you have detected duplicate records with the same email address which is something you don’t want to happen and should have a consequence right?
the other condition is:
} else { // Essentially this verifies a matching record
SO your code would look something like (note: something like not necessarily exactly like ;-)) this
// prompt the user to log in
wixUsers.promptLogin({ "mode": "login" })
.then((user) => {
userId = user.id;
return user.getEmail();
})
.then((email) => {
// check if there is an item for the user in the collection
// PER JEROME'S SUGGESTION
console.log('User Email Address is:'+email);
userEmail = email;
return wixData.query("Profile").eq("email", userEmail).find();
})
.then((results) => {
// if an item for the user is not found
// PER JEROME'S SUGGESTION
console.log('Profile records returned for '+userEmail+' = 'results.items.length);
if (results.items.length === 0) {
// create an item
const toInsert = { "_id": userId, "email": userEmail};
// add the item to the collection
wixData.insert("Profile", toInsert)
.catch((err) => {
console.log(err);
});
} else if (results.items.length > 1) {
// You may want to do something else here
console.log('Oh no we have too many ['+results.items.length+'] Profile records for '+userEmail);
} else {
// In production code when you have finished debugging the page
// This else clause could be removed
console.log('We have seen this user before and things are good!');
}
// update buttons accordingly
$w("#register").label = "Logout";
$w("#showdashboard").show();
})
.catch((err) => {
console.log(err);
});