I need urgent assistance and I have been trying to solve this problem the entire day without avail.
I have two database collections, "Employers" and "Employees". After the user registers to my website their email and ID is automatically added to the appropriate collection. And, on login, the user, whether Employer or Employee is to be directed to the appropriate dashboard based on their ID which is stored in the collection. However, my code is messed up.
As long as a user is registered on my site they are being directed to the first dashboard ("/members-all") which means the data query isn't working as it should. Also, my error message which once showed when there was no record of that id in any collection no longer appears.
You can find my code below... Any help assistance with solving this ASAP
would be greatly appreciated!
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixData from 'wix-data';
$w.onReady(function () {
$w('#login').onClick(function () {
let email = $w('#loginEmail').value;
let password = $w('#loginPassword').value;
wixUsers.login(email, password)
.then(() => {
if (wixUsers.currentUser.loggedIn) {
let UserId = wixUsers.currentUser._id
wixData.query("Employers")
.eq("_id", UserId)
.find()
.then((results) => {
if (results.length > 0) {
wixWindow.lightbox.close('#login')
wixLocation.to("/members-all");
} else {
wixData.query("Employees")
.eq("_id", UserId)
.find()
.then((results1) => {
if (results1.length > 0) {
wixWindow.lightbox.close('#login')
wixLocation.to("/Employee-Dashboard")
.catch((err) => {
console.log(err);
if ($w("#errorText").hidden) {
$w("#errorText").show();
} else {
$w("#errorText").hide();
}
})
}
})
}
}
)
}
})
})
})
export function button19_click(event) {
wixUsers.promptForgotPassword()
}
Hi everyone! Just wanted to share the solution to the issue posted above. The problem was not the query itself as previously thought but was in fact the current logged user function.
Here's the final code below:
import wixUsers from 'wix-users'; import wixLocation from 'wix-location'; import wixWindow from 'wix-window'; import wixData from 'wix-data'; $w.onReady(function () { $w('#login').onClick(function () { let email = $w('#loginEmail').value; let password = $w('#loginPassword').value; wixUsers.login(email, password) .then(() => { console.log("User is logged in") wixData.query("Employers") .eq("email", email) .find() .then((results) => { if (results.length > 0) { wixWindow.lightbox.close('#login') wixLocation.to("/members-all"); } else { wixData.query("Employees") .eq("email", email) .find() .then((results1) => { if (results1.length > 0) { wixWindow.lightbox.close('#login') wixLocation.to("/Employee-Dashboard") } }) } }) }) .catch((err) => { console.log("User is not logged in"); if ($w("#errorText").hidden) { $w("#errorText").show(); } else { $w("#errorText").hide(); } }) }) }) export function button19_click(event) { wixUsers.promptForgotPassword() }
@anthonyb
Just tested the code,
It doesn't seem to be working, it's still directing me to "/members-all" for Employees.
Here's my registration code that I just updated:
import wixUsers from 'wix-users'; import wixLocation from 'wix-location'; import wixData from 'wix-data'; $w.onReady(function () { $w('#register').onClick(function () { let email = $w('#email').value; let password1 = $w('#password').value; let password2 = $w("#confirmPassword").value; if (password1 === password2) { console.log("They match!") wixUsers.register(email, password1) .then(() => { if (wixUsers.currentUser.loggedIn) { let userId = wixUsers.currentUser._id // to get the user id. const toInsert = { "email": email, "consent": $w("#checkbox1").checked, "_id": userId }; wixData.insert("Employers", toInsert) // or "Supplier", toInsert... .then(() => { wixLocation.to('/paypal-integration-employers') .catch((err) => { console.log("They don't match") if ($w("#errorText").hidden) { $w("#errorText").show() } else { $w("#errorText").hide(); } }) }) } }) } }) })
Britney,
To me, it looks like you are expecting the unique key (_id) for a given user to be the same as the unique key (_id) in the two collections that you defined: Employers and Employees. That's not going to be case. If you are going to make this work, you will need to store the Wix-generated user ID for any given user in a separate field in Employers and Employees and call it say "wixuserid". Then when you query Employers, for instance, it would look like this:
wixData.query("Employers") .eq("wixuserid", UserId)
.find() ...
I hope this helps.