Wix database query, Keeps returning an error

Currently im trying to to check the database members for a user id for a user that is logged in

let user
user = wixUsers.currentUser;

let userId;
userId = user.id;

if(wixUsers.currentUser.loggedIn){
wixData.query(“Members”)
.eq(“_id”, userId.toString())
.find()
.then( (results) => {
let items = results.items;
let item = items[0];
let yesOrNo = item.accountSetup;
accountSetup = yesOrNo;
console.log(accountSetup);
} )
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
} );
}

But i keep getting this error every time.

TypeError: Cannot read property ‘accountSetup’ of undefined

that values exists in the database, and is the correct name/format. I have troubleshooted it down to this line.

.eq(“_id”, userId.toString())

essentially it doesn’t return any items. i have no clue why, would help heaps if i could get some help on it.

Hi Jake,
accountSetup is undefined because you didn’t declare it.
userId is already a string. no need toString() method.
Roi

even when i remove the .tostring() function it still breaks and returns the same error

You need to handle the error.
Roi

im not to sure what you mean,

TypeError: Cannot read property ‘accountSetup’ of undefined

it repeats this no matter what.

I ahve provided the images just incase


I have changed the code to this

if(wixUsers.currentUser.loggedIn){
wixData.query(“Members”)
.eq(“_id”, userId)
.find()
.then( (results) => {
let items = results.items;
let item = items[0];
let yesOrNo = item.termsAndCon;
accountSetup = yesOrNo;
console.log(accountSetup);
} )
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
} );
}

To make up for the value that im checking

Comment these lines:
accountSetup = yesOrNo;
console.log(accountSetup
And try again.
Roi

Commented the lines,
still receive this error

TypeError: Cannot read property ‘termsAndCon’ of undefined

Can you please a link to your site a specify the name of the page so we can inspect ?
Roi

the website is https://www.collaborativesounds.com

the name in the wix site manger is
collaborativesounds

and the page code, is on the site side of the code.

did you find a fix?

Hi Jake,
Where is the part of the code that insert the user to the Members database collection ?
Roi

On the sign up page.

export function SignUpButton_click(event, $w) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
} );
}
// user is logged out
else {
let userId;
let userEmail;

// prompt the user to log in  
wixUsers.promptLogin( {"mode": "signup"} ) 
  .then( (user) => { 
    userId = user.id; 
    return user.getEmail(); 
  } ) 
  .then( (email) => { 
    // check if there is an item for the user in the collection 
    userEmail = email; 
    return wixData.query("Members") 
      .eq("_id", userId) 
      .find(); 
  } ) 
  .then( (results) => { 
    // if an item for the user is not found 
    if (results.items.length === 0) { 
      // create an item 
      const toInsert = { 
        "_id": userId, 
        "email": userEmail 
      }; 
      // add the item to the collection 
      wixData.insert("Members", toInsert) 
        .catch( (err) => { 
          console.log(err); 
        } ); 
    } 
    waitToGo(); 
    // update buttons accordingly 
    //wixLocation.to(`/Members/${wixUsers.currentUser.id}`) 
    //$w("#loginButton").label = "Logout"; 
    //$w("#profileButton").show(); 
    
    
  } ) 
  .catch( (err) => { 
    console.log(err); 
  } ); 

}
}

This is the code used to insert and signup