Query find() searchForDuplicates

Urgent ! Thank you in advance
Please help me, i don’t undestand why my function with find() query doesn’t work.
In backend/tools.jsw i have this function :

import wixData from ‘wix-data’;
export function searchForDuplicates(uID) {
wixData.query(“Process”)
.eq(“LoginMail”, uID)
.find()
.then( (results) => {
if (results.length > 0)
{
return “profile-found”;
}
return “profile-not-found”;
})
. catch ( (err) => {
return msg;
} );
}


In my code page i have this code :


import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import {searchForDuplicates} from ‘backend/tools’;
import {getUserEmail} from ‘backend/secureModule’;

$w.onReady( function () {
//TODO: write your page related code here…
wixWindow.scrollTo(370, 370);
});

export function start_click(event) {
//Check if the user already logged in
let isLoggedIn = wixUsers.currentUser.loggedIn;
if (!isLoggedIn) { wixUsers.promptLogin(); }
else {
//Check if the user already has a profile in the profiles database
searchDuplicates();
}
}

export function searchDuplicates() {
let uID = wixUsers.currentUser.id;
return searchForDuplicates(uID)
.then( (msg) => {
console.log(msg); // I have “undefined” but i don’t undestand why
if (msg === “profile-found”)
{
wixLocation.to(“/profilefound”);
}
else if (msg === “profile-not-found”)
{
wixLocation.to(“/createprofile”);
}
})

. **catch** ( (error) => { 
} ); 

}

Hello

can you provide your website link so i can take a look at it

Thanks
Massa

Hello Massa
I do not have an address for the site I’m just creating it.
I just wanted if a user wants to create a profile in the database to first look if there is already a profile with the same email address before creating it. for that I create a function in backend which is in charge to verify if there are duplicates then returns the result to the calling function controlled by the click of the user. Unfortunately this function always sends me the undefined value. I have exactly the same code I sent above
when i use only key word .find() i have 2 as result
when i use .eq(“LoginMail”, uID) .find() i have undefined as result

Thank you.

I just need to know what wrong in my code. for me the function must send me the good result.

Hey
to check if query works test results.totalCount() to see number of records returned. First make sure the query works then when that works you can move forward.

Hello Andreas and thank you
here is my function with display element received and result and all work right

export function searchForDuplicates(uID) {
//let uID = wixUsers.currentUser.id;
let userID = uID;
console.log("searchForDuplicates() from tools module userID : " + userID);
wixData.query(“Process”)
.eq(“LoginMail”, userID)
.find()
.then( (results) => {
console.log("results.length : " + results.length); // here the result is 1
if (results.length > 0)
{
let pf = “profile-found”;
return pf;
}
let pnf = “profile-not-found”;
return pnf;
})

. catch ( (err) => {
let msg = err + " : error searchForDuplicates() from tools module";
return msg;
//throw(err);
} );
}

and here is the code when un click my button

export function searchDuplicatesBtn_click(event) {
let uID = wixUsers.currentUser.id;
console.log(uID);
return searchForDuplicates(uID)
.then( (retVal) => {
console.log(retVal); // here I receive undefined
if (retVal === “profile-found”)
{
return getUserEmail()
.then( (email) => {
let userEmail = email;
let dataObj = {
status: “success”,
email: userEmail
};
wixWindow.openLightbox(“Message”, dataObj);
} )
. catch ((err) => {
//return err;
let dataObj = {
status: “error”,
msgError: err
};
wixWindow.openLightbox(“Error”, dataObj);
} );
}
else if (retVal === “profile-not-found”)
{
wixLocation.to(“/registrering-se”);
}
})

. **catch** ( (error) => { 

let errorMsg = error.message;
let code = error.code;
let msg = "error " + code + ": " + errorMsg;
let dataObj = {
status: “error”,
msgError: msg
};
wixWindow.openLightbox(“Error”, dataObj);
} );
}

Thank you a lot

Ok and you suspect what part not working?

this i think
.then( (retVal) => { console.log(retVal); // here I receive undefined

When I click the button I receive in debug view these values :
5ae8dfec-d107-4d74-8090-a2b0da60c111
undefined
results.length : 1
results.totalCount : 1
results.length : 1
results.totalCount : 1

I don’t know why he shows me the results twice

Hello Thomas,
The problem with your code might be that the values is not being passed in the right way, try using await and async in order to make sure you are getting the values passed correctly, here’s a code example of how to use await and asyn in your code:

export async function searchForDuplicates(uID) {
 //let uID = wixUsers.currentUser.id;
 let userID = uID;
    console.log("searchForDuplicates() from tools module userID : " + userID);

 let results = await wixData.query("Process").eq("LoginMail", userID).find()
    console.log("results.length : " + results.length); // here the result is 1

 if (results.length > 0) {
 let pf = "profile-found";
 return pf;
    }

 let pnf = "profile-not-found";
 return pnf;

}

export async function searchDuplicatesBtn_click(event) {
 let uID = wixUsers.currentUser.id;
    console.log(uID);

 let retVal = await searchForDuplicates(uID)
    console.log(retVal); // here  I receive undefined 

 if (retVal === "profile-found") {
 let email = await getUserEmail()

 let userEmail = email;
 let dataObj = {
            status: "success",
            email: userEmail
        };

    } else if (retVal === "profile-not-found") {
        wixLocation.to("/registrering-se");
    }

}

If you can provide me with your editor link that would be so helpful in order for me to help in debugging the code and fine where the problem exactly is.

Best!
Massa

Firstly thank you for taking the time to come back to me, it is appreciated :slight_smile:

When I preview my page everything works because the user ID is the admin of the database. Unfortunately when I launch the published site and I log in as a member of the site not a admin, I have this error : error WD_PERMISSION_DENIED: The current user does not have permissions to read on the Process collection.
I tried to see the permissions in the DB but wix does not show me anything and I do not know why. I’m still waiting a day or two to access the permissions of the DB via wix editor.
could you tell me please what I need to put as permission at the DB level.

Thank you so much
Best!
Thomas

Hello @thomasiyad

Make sure of your collection permissions:

change the permission to based on your website needs (go to your collection → click on permissions → edit permissions)

Best
Massa