Hi,
Im having an issue with creating a query on a user id.
Im trying to get the current user id and then check in the database if this id does exist already.
The code seems right but even if the id is in the database the results return 0.
The databse has an automatic _owner field which holds the user id and Im trying to eq this field with userId.
Would really aprreciate any help!
Here’s the code Im using:
import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
import wixWindow from ‘wix-window’
export function button1_click(event) {
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true
let userRole = user.role; // “Member”
user.getEmail()
.then((email) => {
let userEmail = email; // “user@something.com”
});
// user is logged in
if (user.loggedIn) {
wixData.query( “PlanEvents” )
.eq( “_owner” , userId)
.find()
.then((results) => {
if (results.items.length === 0 ) {
wixWindow.openLightbox( ‘MembersOnly’ );
@ajithkrr
Im not really sure what should I see in console… I just see that it writes the word “nothing”.
thats what I see there:
Loading the code for the site. To debug this code, open masterPage.js in Developer Tools.
Loading the code for the course page. To debug this code, open nfsfz.js in Developer Tools.
Site Code
Line 8 Nothing
course Line 27 Loading the code for the MembersOnly page. To debug this code, open u1olo.js in Developer Tools.
Just keep getting the lightbox all the time because reasults are 0.
Thanks!
So I noticed something weird here… first you were right there is no match between the 2 id’s. So I created a userid field the will hold the current user id for the query to run properly. The problem is that its not inserting the current user id. Each time its inserting my admin user id to the owner and to the userid field.
Here is the backend code:
/*****************************
Backend code in events.js *
*****************************/
// For inserting data into a collection. import wixData from ‘wix-data’ ; import wixUsersBackend from ‘wix-users-backend’ ;
// The onPlanPurchased() event is fired when a plan
// is purchased, or a free plan is ordered.
// Get the order information
// from the event’s order object. let user = wixUsersBackend.currentUser; let userId = user.id; // “r5cme-6fem-485j-djre-4844c49” let isLoggedIn = user.loggedIn; // true
user.getEmail()
.then((email) => { let userEmail = email; // “user@something.com”
});
export function wixPaidPlans_onPlanPurchased(event) { if (event.order.price.amount === 0 ) { let orderData = {
“title” : “Free plan purchased” ,
“data” : event.order, “userid”: userId
};
wixData.insert( “PlanEvents” , orderData);
} else { let orderData = {
“title” : “Premium plan purchased” ,
“data” : event.order, “userid”: userId
};
wixData.insert( “PlanEvents” , orderData);
}
}
…and →
If you are doing all these process in the preview mode… it will only insert your admin id…
But if this is happening on your live site, then try logging in with different account …
Ok so now I know what the problem is.
Seems like the code does not really gets the current user’s Id
The current user ID in the collection does not match to the logged in member’s ID. The ID in the collection is always my admin’s ID even if im on the live site. I tried to log in with different accounts but every time its inserting the same ID (my admin’s id) to all items…
The Id on the text is really the user id I need to insert. But still, when trying to insert the user id to the collection after an onpurchase event its not inserting that user Id its inserting my admins Id.
I guess its something in the backend code that’s not getting right the user’s id.
I tried to change the user.id line to this one let userId = wixUsersBackend.currentUser.id;
But still not inserting the current user’s id.
If I’m inserting manually to the collection the id that comes from console so the process works but the problem is that the Id is not being inserted to the collection.
Ok so I managed to solve this, The solution is to add the: event.order.memberId to the userid field in the collection after the Onpurchase event.
@ Ajit Kum ar Thanks for your kind help!!