Error: ItemId must be a string

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

1 Like

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.

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.

What fields does the database have? Are you sure the error occurs on the query you posted? Or do you have other queries that might have caused it?

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.

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.

Page: Meu Orçamento (/meu-orcamento)

Hi @yisrael-wix ! Do you have any update or questions that I may help you?

@ricardo The permissions in your database collections do not permit a site visitor to write to the collections. See: About Collection Permissions

@yisrael-wix but I have used { “suppressAuth”: true }. Isn’t that supposed to allow site visitor to write to the collections?

Btw, I have tested the colletions changing permissions to allow visitors write, read and create and still have same problems.

@ricardo

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.

@yisrael-wix

Ok. I’m sorry.

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.

Please, check again.

Best regards,

@ricardo

That is great - much easier now…

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:


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).

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!

You have an interesting point, but the console.log() returns null for user.id. It does return a User object, but it’s more or less empty.

Note that there are also anonymous visitors - that is, they aren’t “users”, or they aren’t considered an “official” site user.

@yisrael-wix

Thank you very much again. Is there any documentation I can go deep into this matter? I really need learn all possible cases for users.

Best regards

@ricardo

The API is the source. wix-users and wix-users-backend

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.

user. id, is not a string as it does not have “”. Also you would want to make sure there is an actual variable named user .id.