Hello everyone,
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.
Regards
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:
As you can see, the id is null. That means there is no current user, and the database query fails.
That's why you're getting the ItemId must be a string. In the case that there is no user, you are doing a query with null (which is not a string).
I know this is old, but the parameters for the function:
wixData.get("DatabaseName",itemId");
are "DatabaseName" and "itemId". A string is enclosed with "" as noted.
Wow, thank you very much!
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.
"
Best regards!
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.
I notice that this problem is related to all my databank. I have other queries like this in this page.
Only If I comment all these code lines related to the databank the problem disappear.
Thanks for reply!
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 }. Still not undestanding whats is wrong.
Did you sync your sandbox database to the Live database?
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.