How to access an element in an array?

Hello, I’m trying to figure out how to work with arrays in wixcode, specifically accessing the elements in that array.

Say we have this database
| u_first | u_last | u_type | u_score | User (ID) |
| A | A1 | AA | 10 | 101 |
| B | B1 | BB | 4 | 102 |

I’m trying to create a function that when a user’s logged into a site, it will

  • Search through this database for their currentuser.ID.
  • The function will then store the result in an array that I can then use to display information on the website on their account page.

This is what my function currently looks like:

let userData = {};
wixData.query(“users”)
.eq(“Users (ID)”,wixUsers.currentUser.Id)
.find()
.then( (results) => {
if (results.items.length > 0) {
let details = results.items[0];
userData.first = details.u_first; //This is where I’m not sure about…
$w(“#text34”).text = userData.first; //text34 is supposed to display the user’s first name
}
})
. catch ( (err) => {
let errorMsg = err;
});

Assuming the user with first name A logs into the website, the function would use their currentuser.ID to search the database for their data row. This will then be stored in “details”, and then text34 element will be changed to their first name (which in this case should be “A”). But I’m not sure if my code is correct to do that.

Thanks in advance.

Hi Shiro,

The first thing to do is to be sure your query is returning data. I see a problem with it where I know it can’t be returning anything. Be sure that you are using the field key from the users collection. In the screen shot below, the field key is “roomType” and that is what wixData.query uses.

Secondly, the ID field in there would have to be the same ID that the Members/PrivateMembersData has because that is where wixUsers.currentUser.Id is looking to obtain the ID.

Put a console.log(results) after the .then line, so you know whether or not the query is returning any results. Once you see results in your console, expand it and see what’s in there.

Thanks anthonyb! I was using the field name instead of key. I’ll try it out and let you know how it goes. Also, I didn’t create the ID field, it’s just…there…Plus it doesn’t seem to be editable so I’d take a wild guess and say that’s what wixUsers.currentUser.Id is looking for. One thing I’m not sure about is, the ID in the database isn’t a string, but rather a directory, so should I tell the search function to search for whatever is in that field or just the ID value??

Take a look at the Members/PrivateMembersData. Can you find an ID in there that matches the ID field in your custom user collection? I don’t really know where you’ve been with setting this up, so I’m a little in the dark on this. Fact remains, however, that using wixUsers.currentUser.Id in a query means that Wix/Corvad is using the ID field below to search your users collection.

I can’t find that database in my website, the users database is the only database I’ve got…Does this have something to do with the member’s area???

You have to explicitly add it to your site, though apparently you have added your own users collection. You could proceed down the path of just using your custom users collection. In which case, your query should be referencing that ID and not the one from wixUsers.

It sounds like you should read the following article to see if you think you should use the members area. If you are going to use wixUsers.currentUser.id you will need to learn more about what the members area can do.

https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area

I decided to not use the member’s area because I couldn’t figure out how I can add other things in (display user’s score, age, gender etc). I found there was only so much I could display with that I strayed away from that. I could’ve been blind and missed those things entirely though…

Ok, I’ve figured it out. You were right. wixUsers.currentUser.Id looks for the id in the PrivateMembersData database that I can only find after adding a members area. Now my next problem comes up. I’ll post it in a different post. Thanks alot for your help @tony-brunsman