I have made a website that is working perfectly in preview mode, but comes with some mistakes when I visit the published version. One of them is about this error message that comes from a database query: “Error: ItemId must be a string.”
Can someone help me?
Find here below my code:
Backend:
export function x() {
let user = wixUsers.currentUser return wixData.get(“database”, user.id, { “suppressAuth”: true }).then(data => { return false
})
}
Btw, all my data bank is accessible only by admin, but I used { “suppressAuth”: true } in this case so that regular users can access it by my front end.
The other possibility is that the wix-users API is only partially functional in Preview. When you run your site in the Editor, the current user is you . However, when running the Live site, you need to have a user - and it sounds almost as if there is no current user.
Did you sync your sandbox database to the Live database? No, my live databse is supposed to be empty righ now.
However, when running the Live site, you need to have a user - and it sounds almost as if there is no current user. I’m not sure if I understood what you said, but I’m indeed not a member of the live site. Because of that I used { “suppressAuth”: true }.
Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor.Please include the name of the page involved.
Your app is very large, with a lot a lot of code and collections, and I just can’t find where ItemId is.
Understand that we are unable to debug or rewrite complex code and page configurations. If you feel that there is a bug in Wix Code, then please try to create a test page with the minimum scenario in which the problem appears to help us investigate the issue properly.
I have done it. I commented all code lines except by those that matters. I also translated some functions from portuguese to english to help you to understand what I’m trying to do. I hope now you can better understand what is going on.
In this page we had 2 different pieces of code triggering that message. I have isolated one of them so that you can analyse.
I added some comments to you code below to indicate what I think is the problem.
export function authorizedUser() {
let user = wixUsers.currentUser;
if (user.loggedIn) return true;
// if it gets here, the user is not logged in
// but what happens if there is no user?
// user.id will then be "garbage", and that might be the problem
console.log(user.id); // what's displayed here?
console.log(user); // you can also inspect the entire user object
return wixData.get("Membros", user.id, { "suppressAuth": true }).then(membro => {
Let me know how it goes. If that’s not it and you’re still having trouble, I’ll clone your site, publish it, and then test it out.
OK, this was bothering me, so I cloned and published. As I pointed out in my comments, if you do a console.log(user.id), you’ll end up with something like this:
One last thing: I thought that a ‘user’ (with an id) would be assigned to any visitor, no matter what. When/how does it happen (visitor not having a user)?
From API I found only this definition below:
"
The wix-users-backend module contains functionality for working with your site’s users from backend code.
There are three types of users:
Visitor: A user who is not logged into your site.
Member: A user who is logged into your site.
Admin: The owner of the site.
"