# Attempting to Access Reference Fields by Code Returns Undefined

**URL:** <https://forum.wixstudio.com/t/attempting-to-access-reference-fields-by-code-returns-undefined/21277>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [May 25, 2020, 6:27am UTC](https://forum.wixstudio.com/t/attempting-to-access-reference-fields-by-code-returns-undefined/21277 "2020-05-25T06:27:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [May 25, 2020, 6:27am UTC](https://forum.wixstudio.com/t/attempting-to-access-reference-fields-by-code-returns-undefined/21277/1 "2020-05-25T06:27:01Z")

</div>

Hi everyone 🖐

I’ve been trying to access a reference field by code, I think that the method I use used to work, but now I get “undefined”.

Here’s the code:

```javascript
$w('#articlesDataset').getItems(0, 7).then( (result) => {
                let items = result.items;
                let totalCount = result.totalCount;

                for (let i = 0; i < totalCount; i++) {
                               let label = items[i].title;
                               let tKey = `${language}_Title`;
                               let translatedLabel = items[i].translate.tKey;
                               console.log(items[i], translatedLabel)
            }
        })

```

 ![](https://us1.discourse-cdn.com/wix/original/3X/5/f/5f35fc6f25ba023b3d2b395dd4bd78eabc1048b2.png)

“translate” is the ID of the reference field in the main database.  
`${language}_Title` is the field ID in the second collection that has a reference field to represent it in the first collection, for example: “he\_Title”, “de\_Title” and so on.

Perhaps I’m using the wrong way, so what’s the right way to do it?  
I appreciate any help guys.

Thanks.  
Ahmad

---

<div class="post-metadata">

**Author:** ![anhelinak](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/anhelinak/32/3202_2.png) [@anhelinak](https://forum.wixstudio.com/u/anhelinak)\
**Post date:** [May 25, 2020, 10:11am UTC](https://forum.wixstudio.com/t/attempting-to-access-reference-fields-by-code-returns-undefined/21277/2 "2020-05-25T10:11:09Z")

</div>

I recommend you to use wix-data [query()](https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html) along with [include()](https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#include) in order to query all of the items including the referenced ones.  
If you want to get only the referenced items, then you should use [queryReferenced()](https://www.wix.com/corvid/reference/wix-data.html#queryReferenced)

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [May 25, 2020, 10:37am UTC](https://forum.wixstudio.com/t/attempting-to-access-reference-fields-by-code-returns-undefined/21277/3 "2020-05-25T10:37:59Z")

</div>

Hi @anhelinak , thank you for your suggestion.

Why do I have to query the collection? The items are already there from the dataset, so it should be just a matter of accessing the reference field.

As a way around to avoid making major changes to the actual code, I used the dataset to get the ID ( **\_id** ) or the referenced field and then query it and return the data, it’s just a few more lines (8-12) for each, but it’s a lot of lines to add for each dataset, this method should be built-into the dataset API.

```javascript
function getReferencedItem(id, referencedCollection) {
       return wixData.query(referencedCollection).eq('_id', id).find().then((result) => {
        if (result.items.length > 0) {
               let item = result.items[0];
               return item;
        } else {
               return {'type': 'Error', 'message': "Couldn't retrieve the Item"}
        }
    }).catch((err) => {
        return {'type': 'Error', 'message': "Couldn't retrieve the Item"}
    })
}

```

That’s the easiest way I came out with, in order to achieve the same result without changing a lot of the code structure.

What do you think? And is there any simpler ways?

Thanks.  
Ahmad
