Reference fields for UX only?

I have 2 collections connected by reference field but I can’t get the fields to resolve when I do a wix query and testing the code. All the examples I see have to do with displaying the data on a repeater or table.

Are reference fields only for displaying data on repeaters and tables etc…?

My setup:
Collection1: Primary key ‘email’ (this collection has another single reference field to another collection)
Collection2: Reference field to Collection1, also named ‘email’

return wixData.query("Collection1")
    .include("Collection2")
    .find()
    .then((results) => {
        results.totalCount
        console.log(results.items)
    })
    .catch((err) => {
        let errorMsg = err;
        console.log("Returned nothing")
    });

The above code only returns Collection1 data but I’m looking to get Collection1 and the data from Collection2 for that item in Collection1.

Thoughts?

I’ve even tried the reverse, querying Collection2, including Collection1 but same result, only showing data for Collection2.

When using .include, you need to specify the ID of the field, not the Collection. From the looks of it, this seems to be the ID of the other collection and not the field. I don’t know what the ID of your referenced field is, but it should be something like:

return wixData.query("Collection1")
    .include("reference")

Additionally, you can also try giving this a shot:

This is correct, I was referencing the collection vs the reference field.

Thank you.

1 Like