My website’s code was working perfectly until someday it gives some problems. My code is essentially failing in one specific part. I nee to get the user info ( wixdata.get) and then need to update the ip associated with that user (wixdata.update). It worked before, but now it is not updating the ip and the website keeps on failing.
Here is that piece of code that fails of my website (www.wikicai.com):
wixData.get("Users", userEmail)
.then((item) => {
item.ip = ip // updated last id
let cambios = item.cambiosip
item.cambiosip = cambios + 1,
wixData.update(“Users”, item);
})
. catch ((err) => {
let errorMsg = err;
});
wixLocation.to(“/anti-sharing”);
wixUsers.logout()
Btw, the last two lines of code work because the website actually gets the user to the anti-sharing page and logs it off.
Please help, I cannot tolerate my website to be unusable as my users pay to visit my content.
Thanks.
Suddenly? Sorry, but I’m not sure how it ever worked. Your code has some errors:
In this snippet, you incorrectly have a comma at the end of the first line.
item.cambiosip = cambios + 1,
wixData.update("Users", item);
Also, the last two lines get executed before the update ever occurs. You should include them in the Promise’s then() function:
wixData.get("Users", userEmail)
.then((item) => {
item.ip = ip; // updated last id
let cambios = item.cambiosip
item.cambiosip = cambios + 1;
wixData.update("Users", item);
wixLocation.to("/anti-sharing");
wixUsers.logout()
})
.catch((err) => {
let errorMsg = err;
});
I hope this helps.
I managed to modify all the code and made this:
let toUpdate = { “_id”: userEmail,
“idusuario”: idusuario,
“ip”: ip,
“email”: userEmail,
“cambiosip”: cambiosip + 1,
};
let options = {
“suppressAuth”: true ,
“suppressHooks”: true
};
wixData.update(“Users”, toUpdate, options)
. catch ((err) => {
let errorMsg = err;
});
wixLocation.to (“/anti-sharing”);
wixUsers.logout()
}
Do you think it is better now? How can I work with the number cambiosip. I want to update it every time the code enters this if statement and add to the previous number one digit.
Thanks.
@frandullera Now you don’t have a .then() function which is the Promise fulfillment.
You should read the following regarding Promises:
@yisrael-wix I think I do have that done right as I have a then function before the code I uploaded before. Here is the full code so you can get a better glance.
$w.onReady( function () {
let user = wixUsers.currentUser;
let idusuario = user.id; //
let isLoggedIn = user.loggedIn;
var cambiosip
getIpAddress().then((ip) => {
user.getEmail()
.then((email) => {
let userEmail = email; // “user@something.com”
wixData.query(“Users”)
.eq(“idusuario”, idusuario)
.find()
.then((results) => {
if (results.totalCount > 0) {
if (results.items[0].ip === ip) {
} else {
let toUpdate = {
“_id”: userEmail,
“idusuario”: idusuario,
“ip”: ip,
“email”: userEmail,
“cambiosip”: cambiosip + 1,
};
let options = {
“suppressAuth”: true ,
“suppressHooks”: true
};
wixData.update(“Users”, toUpdate, options)
. catch ((err) => {
let errorMsg = err;
});
wixLocation.to(“/anti-sharing”);
wixUsers.logout()
}
} else { //logging in for first time
let toInsert = {
“_id”: userEmail,
“idusuario”: idusuario,
“ip”: ip,
“email”: userEmail,
“cambiosip”: “0”,
};
wixData.insert(“Users”, toInsert)
.then((result1) => {
let item = result1; //see item below
})
. catch ((err) => {
let errorMsg = err;
});
}
}
);
})
})
})
@frandullera There is no .then() in the following section of code:
wixData.update("Users", toUpdate, options)
.catch((err) => {
let errorMsg = err;
});
wixLocation.to("/anti-sharing");
wixUsers.logout()
The last two lines of code will execute before the update. You need to make sure that your code works with Promises or your code simply will not work. Please review the articles I posted to see how Promises work.
Didn’t you say that this worked before?