Reference field returned as object vs. _id

Hello Wix friends,

I’ve noticed something weird in my code. Here’s the situation:

userDB = a collection containing user information
dbA = a collection which contains a “user” field referenced to userDB
dbB = a collection which contains a “user” field referenced to userDB
dbADataset = a dataset linked to dbA
dbBDataset = a dataset linked to dbB

When I say:

let dbAItem = $w(“#dbADataset”).getCurrentItem()
let user = dbAItem.user

…then I get user = a string (i.e. the _id of the user item in userDB)

When I say:

let dbBItem = $w(“#dbBDataset”).getCurrentItem()
let user = dbBItem.user

…then I get user = an object (i.e. the corresponding item in userDB)

Does anyone have an idea why this would be the case? The “user” fields in both dbA and dbB are referencing the same underlying userDB.

I’m stumped :frowning:

Hello.

Please note that the value that is displayed in the reference field comes from the Main field of the specific item in the referenced collection. The actual value that is stored in the database is the ID of the referenced item (in other words, the value in the ID system field of the referenced collection).

This simply means that the reference field is an object since it gives access to all fields(attributes) of the referenced item.

To learn more, check out this article: About Reference Fields in Database Collections

Good luck!

Did you ever get an answer to this? I have the same issue right now. Database A has 3 reference fields to Databases B, C and D, but I’m only getting an item returned from Database B. C and D only give me the _id

This is a year old thread. Per guidelines, bumping old posts is not done. Please create a new post. And add more info/code, so we can help you better.