[RESOLVED] wixData.update code being skipped

I’ve been trying to use the wixData.update code to update a database on button click on my page and it seems like the code is skipped entirely every time. See below snippet:

$w(“#button8”).onClick( (event) => {
$w(‘#text23’).show();
wixData.query(“Registered_yes_no”)
.eq(“email”, userEmail)
.find()
.then( (results) => {
let item = results.item;
let rId = item[0].id;
console.log(rId);
let toUpdate ={
“_id”: item[0].id,
“registered”: true ,
“email”: item[0].email
};
wixData.update(“Registered_yes_no”, toUpdate)
.then( (uResults) => {
let uItem = uResults;

                }) 
                . **catch** ( (err) => { 

let errorMsg = err
})
})
. catch ( (err) => {
let errorMsg = err;
})

userEmail is a variable defined with wixUsers code before the first on.Ready function takes place. I’m not getting anything to show up from my console.log commands. Nothing. I even set permissions for the database to allow anyone to do anything so it can’t be a permissions thing blocking it.
I have code that runs after this in the same onClick event that works with no problems and inserts new data into a separate database but this one section of code refuses to get picked up.
Please help me.

Are you sure, that you queried the users Email right? How does your “get user email” code look like? Can you console.log(userEmail) successfully?

Thanks for the reply. I’m sure I got that correct as I get it to show via console earlier in the code. I’m not looking at the code right now but I’m pretty sure it’s something like:

Import wixUser from wix-user
var user = wixUser.currentUser
var userId = user.id
var userEmail = user.email
console.log(userId, userEmail)

@dwyanekrzanowski okay your “query“ for User E-Mail looks fine. I am also not on a computer right now but I guess there is a typo in your code. It should be results. items instead results.item - Try to console.log(results.items)

@oezdemirumut thanks. I’ll give it a try and see if I have any luck.

@oezdemirumut alright, so I removed the update portion for now and changed the code to this:

$w(“#button8”).onClick( (event) => {
$w(‘#text23’).show();
//change status in registered database to registered
wixData.query(“Registered_yes_no”)
.eq(“email”, userEmail)
.find()
.then( (results) => {
let item = results.items;
var rId = item[0].id;
var rRegistered = item[0].registered;
var rEmail = item[0].email;
console.log("ryndb id = " + rId);
console.log("ryndb registered = " + rRegistered);
console.log("ryndb email = " + rEmail)
})

                . **catch** ( (err) => { 

let errorMsg = err;
})

Now when I get the response for console.log("ryndb id = " + rId) it comes up as undefined.
But, the email value displays correctly and the value for registered displays correctly too.

probably you mean item[0]._id ? Try to console log every variable to see which one is corrupted

Yeah, I tried that too and no luck. I console logged all 3 variables and only id comes back undefined. When I console log the results nothing happens.

I think you are missing the underscore in Id, may be that’s why it’s throwing error
_id is the required field
result.items[0]._id not result.items[0].id

Try this code

$w(“#button8”).onClick( (event) => {
$w(‘#text23’).show();
wixData.query(“Registered_yes_no”)
.eq(“email”, userEmail)
.find()
.then( res => {
if(res.length > 0) {
let item = res.item[0];
item.registered = true;
wixData.update(“Registered_yes_no”, item)
.then(update => console.log("Updated! : " , update))
.catch( err => console.log("Error while updating : " , err))
}
})
.catch( err => console.log("Error while query : " ,err))
});

Is there a need with the options { suppressAuth : true, suppressHooks : true } ?

you missed the s in " let item = res.item s [0]; "

in response to all of the above; first thank you all for your help.
Next @ mauroventoavyno, I shouldn’t need options for that since I have the db completely open regarding permissions and I have no hooks running either.
@SalamanA: I’m going to try the code you provided but if i use .update without redefining every field in the item, won’t that simply delete the undefined field and create a whole new item in the db? The database I’m working with is simply to check to see if the user is registered for an event so as to hide the button that allows them to register when they get back to their status page. All it contains is those three fields; ID (by default), registered (a boolean value), and their loginEmail (the one constant across all the db’s I’m using). I need to use their loginEmail (userEmail [in this case they will always be the same even though they have different fields in the membersprivatedata db]) to find their respective item in the Registered_yes_no db and I need to ensure that .update actually updates their respective item as opposed to creating a new one otherwise the code to check if they’re registered won’t work.
@umutOezdemir: I added the s.

I’ll let you know how your code works out for me soon.

OK so I tested your code and had to modify it to include the undefined fields and it worked!
Here’s what I ended up with:
$w(“#button8”).onClick( (event) => {
$w(‘#text23’).show();
wixData.query(“Registered_yes_no”)
.eq(“email”, userEmail)
.find()
.then( res => {
if (res.length > 0) {
let item = res.items[0];
let rId = item._id;
console.log(rId);
let rRegistered = item.registered;
console.log(rRegistered)
let rEmail = item.email;
console.log(rEmail);
let toUpdate = {
“_id”: rId,
“registered”: true ,
“email”: rEmail
}
console.log(toUpdate)
wixData.update(“Registered_yes_no”, toUpdate)
.then( update => console.log("Updated! : " , update))
. catch ( (err) => {
let errorMsg = err;
console.log("Error while updating : " , err)
})
}
})
. catch ( (err) => {
let errorMsg = err;
console.log("Error while query : " ,err)
})
It finally updated the proper item and the console.log commands worked as they were supposed to.
I did get an uncaught error in the console so I adjusted the way I wrote the .catch code to what you see above in order to hopefully amend that. The uncaught error didn’t interfere with the functionality though so I’m not too stressed about it.

Thanks again to all of you this has been extremely helpful!

I’ve moved on from this code as I redesigned the databases on my page but I do recall that adding the ‘s’ to results.item’s’ fixed some of the issues if not all.

Thank you all for the help and quick feedback.