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()
}
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.
@tony-brunsman Thanks, for assisting.
I would need to update my code for the registration form now and I’ll let you know if it works but is the remainder of the code correct? Also, would this solve the issue of my error code not appearing?
@tony-brunsman
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();
}
})
})
}
})
}
})
})
@brit-alexis it’s hard to tell from the remainder of the code, but it looks to be pretty solid. You won’t know for sure until you are returning reliable results from the Employer and Employees queries. Then you can tweak the rest of it, if needed.
@tony-brunsman Okay, thanks.
@Britney, if you have been inserting in like fashion all along, you would have the same user ID value in the _id field of Employers and Employees. Consequently, those queries in your first post should return reliable results.
I need to take what you are saying at face value and state the obvious here because your code suggests that the very same Employees ID of the user that you are logging in as is in the Employer collection too. Could that be?
@tony-brunsman Hi Anthony,
I’m sorry but can you reword what you said above? I’m not 100% sure what you’re asking.
This is what I gather. Are you asking if I created both accounts to test the functionality and therefore have the same userId for both collections? If that is indeed what you’re asking, I tested that originally and realised that the _id registered in both collections are not the same. Therefore, I assumed that since the code takes the _id of the current logged in user and queries it to the _id registered in the collection, it should work. But for some reason only the first part of the query is functioning.
@tony-brunsman But if you are suggesting, that both queries are querying the same user IDs, then should I query an email for one and an ID for the other?
@brit-alexis The point that I’m trying to make is that the insert function, if it is assigning the _id value, will in fact override whatever _id Wix gives it for the Employer and Employees collections. I didn’t think that it did that. Have you been using insert to get the data into those collections right along in the registration page, or was that a change you just made?
In any event, to your very latest post, an email address would work too since you can count on it being unique for that user.
@tony-brunsman In response to your question, I’ve been using insert to add the email and consent of the user to the collections which are added successfully. The only other insert I added was the _id which is shown in the code I posted. But what I did notice is that the userId didn’t add to the collections in the column you suggested I should create. Is that the issue?
@brit-alexis I guess it all depends on the state of the data. It doesn’t sound like the _id values in Employer and Employees correspond to the actual user id. With insert you can make them the same, but it sounds like you didn’t do it that way. They need to be the same if the queries are going to use _id. Otherwise, create ID fields with a different name in Employer and Employees as previously suggested.
Is this discussion going around in circles yet? Be sure you are viewing the actual _id when you browse the collection. It’s hidden by default. You have to tell Wix to show it. Here’s a screen shot. ID needs to be checked. Pardon me if you’ve already done this.

@tony-brunsman Okay, thanks a lot for your help! That should be enough information to get it working.
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()
}