Hio Brett:
find() will return an array of items that it gets from your .eq() query.
You should test the res.totalCount to see if it is greater than 0 to determine if you have an existing record better yet you should test greater than 1 also as that would be a coding error [;-)] and you would need to figure out how you let duplicates in in the first place.
So I’ve proposed some changes below that use the Promises from these calls more effectively. By returning a function call that returns a Promise you can cascade .then() calls at the same level in your code and terminate the string with a catch for exceptions. You can then use the exception catch for dealing with all errors so when you have a logic error you simply throw and error (exception) which terminates the code flow and jumps you to the catch.
import wixData from 'wix-data';
let user = wixUsers.currentUser;
user.getEmail()
.then( (email) => {
let userEmail = email;
// "user@something.com"
// ******* Return the query find() and handle the then at the same level
// as getEmail. Then use catch foone catch for all errors
return wixData.query('contactInfo')
.eq('#email', userEmail)
.find();
})
.then((res) => {
if(res.totalCount > 1) {
// Internal Error more than one entry exists
throw Error('Internal Error - more than one entry exists');
} else if (res.totalCount === 1) {
throw Error('Duplicate user found, returning');
}
//insert to collection here (no duplicate found)
return wixData.insert("contactInfo",userEmail);
})
.catch((error) => {
//below is error messages to show
$w('#error').show();
$w('#errorBig').show();
$w('#errorText').show();
$w('#errorBubble').show();
console.log(error);
} );
Hope this is helpful. At a minimum you need to check the totalCount ![]()