[solved!] Can't query user id

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’ );

// redirect to purchase
} else
wixLocation.to( “https://ar.faidda.com/hebrewlesson1” );
// update buttons accordingly
});

} 

}

Try this →

export function button1_click(event) {
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn; 
let userRole = user.role; 
    user.getEmail() 
        .then((email) => {
        let userEmail = email; 
               });
               
               if (user.loggedIn) {    
                       wixData.query("PlanEvents")    
                         .eq("_owner", userId)     
                       .find() 
                         .then((results) => {
          if (results.items.length > 0) {  
          wixLocation.to("https://ar.faidda.com/hebrewlesson1");                                                      
     } else {                        
          wixWindow.openLightbox('MembersOnly');
     }
     });
                  }
           }

Hi, thanks for reply. Unfortunately still results shows 0…

@faidda7 Console the result and see what you get …

  wixData.query("PlanEvents")    
                         .eq("_owner", userId)     
                       .find() 
                         .then((results) => {
          if (results.items.length > 0) {  
          let items = results.items;
          console.log(items);
          wixLocation.to("https://ar.faidda.com/hebrewlesson1");                                                      
     } else {                        
          wixWindow.openLightbox('MembersOnly');
          console.log("Nothing");
     }
     });
                  }

@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.

@faidda7 In your database, is there any item ???
Check your _owner field …

If you can, please post the picture of your database…

@ajithkrr I do have an item and Im logging in with the username and password that are connected to that id in the _owner field.

Any other Ideas?

@faidda7 Try consoling the current userId and check if your userId matches the _owner field…

console.log(userId);

@faidda7 And also, try all this in the preview mode…

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);
}
}

"userid": userId

Is “userid” your field key ???

…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…

Try this and check if it works …

Add a text your page…
#text1 - text

$w('#text1').text = userId;

Go to your live site
See what you see in the text…

…now get on with the insert process…
check →
if this id and the admin id matches or not…

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!!

:wink: