How to get items in separate database that are referencing current item?

I have two databases: Projects and Videos.

I would like to get the Videos associated with the current Project.

In the Videos database, I have a “project” field that references the video’s project in the Project database.

Can I use getCurrentItem() on the Project page to access the items in the Video database that reference the current project? Or do I need to use something other than getCurrentItem()?

Hello Nick,

yes you can use “getCurrentItem” if you have your code on a dynamic page.
On dynamic page you have always the current item selected.

If not you have to take …
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getItems

or without dataset…
https://www.wix.com/corvid/reference/wix-data.html#query
…and work with it.

Thank you. So how would I get properties from the separate database that reference the current item?

You have database-A and database-B.
DATABASE-A =PROJECTS
DATABASE-B = VIDEOS

What is the common value between PROJECT and VIDEO?
What is the structure of your DATABASE?

DATABASE-A

PROJECT-1
PROJECT-2
PROJECT-3

DATABASE-B

VIDEO-1 / PROJECT-2
VIDEO-2 / PROJECT-5
VIDEO-3 / PROJECT-3
VIDEO-4 / PROJECT-1
VIDEO-5 / PROJECT-2
VIDEO-6 / PROJECT-4
and so on…

Is your DB-Structure like this one?

Show DB-structure.

Yes my DB-Structure is like that.

DATABASE-B has a reference field connected to an item in DATABASE-A.

Hello again,

so when you are on your dynamic page and you choose one of your projects by clicking, you start the command …

let itemObj = $w("#myDataset").getCurrentItem();

/*  
 *  {
 *    "_id":                "fcec780a-3e37-4b64-8a66-37c552c53f99",
 *    "_owner":             "f6c0f9c3-a62d-7e9f-g58d-943829af244d9",
 *    "_createdDate":       "2017-05-01T17:19:03.823Z",
 *    "_updatedDate":       "2017-05-01T17:19:10.477Z",
 *    "title":              "Dr. ",
 *    "name":               "B",
 *    "link-dynamic-name":  "/myCollection/B"
 *  }
 */

As you can see you will get the current item in form of an object.
The example above demonstrates, what you will find in this object.

You will find all DATA of the CURRENT-ITEM.

In your case, it will be …

 let itemObj = $w("#dynamicDataset").getCurrentItem();

Now you can get all data of the selected object by console-logging it.

console.log(itemObj)
console.log(itemObj.title)
console.log(itemObj._id)
console.log(itemObj._owner)
console.log(itemObj._createDate)
console.log(itemObj._updatedDate)

Not sure, could also be.....

console.log(itemObj.items)
console.log(itemObj.items.title)
console.log(itemObj.items._id)
console.log(itemObj.items._owner)
console.log(itemObj.items._createDate)
console.log(itemObj.items._updatedDate)

and you can also do...
console.log(itemObj.items[0].title)

So if I wanted to access videos that referenced the current page, would it be something like this?

let currentPage = $w("#dynamicDataset").getCurrentItem();

let relevantVideo = $w("#videosDataset")
.filter(v => v.referenceField=currentPage.items.title)

Thank you for your continued help.

Hello again,

try to LOG the OUTPUTS by using CONSOLE.LOG.
Then you will be able to see what you are doing, respective what is doing your CODE.

What do you get if you log this one in the CONSOLE?

console.log(currentPage)

What is the OUTPUT? What did you get? An String? A Number? An Object? Something else?

Maybe it will be an OBJECT.

So if you got no ERROR and instead of it got some data, so it’s a good sign, the code works.

This is how to work with “.getCurrentItem”…

As you can see in this example, it is also shown, what are the RESULTS if you run this pice of CODE. And take just a closer look!
It is even marked as OBJECT in the example! :grin:

/*  itemObj:
 *
 *  {
 *    "_id":                "fcec780a-3e37-4b64-8a66-37c552c53f99",
 *    "_owner":             "f6c0f9c3-a62d-7e9f-g58d-943829af244d9",
 *    "_createdDate":       "2017-05-01T17:19:03.823Z",
 *    "_updatedDate":       "2017-05-01T17:19:10.477Z",
 *    "title":              "Dr. ",
 *    "name":               "B",
 *    "link-dynamic-name":  "/myCollection/B"
 *  }
 */

Ok, you see all these RESULTS here now ask yourself, “how do i get this results”?
Did you ask yourself this question just right now? Don’t lie, you did, i know it xDDDDDDDD.

Ok and here you can see now, how to get these RESULTS. The magic of CONSOLE.LOG. It will be something like this…

console.log(itemObj._id)
console.log(itemObj._owner)
console.log(itemObj.createdDate)
console.log(itemObj._updatedDate)
console.log(itemObj.title)
console.log(itemObj.name)

…and so on.

And here you will get informations how to work with dataset.FILTER…