use data from openlightbox to populate dataset

My journey begins with:

  1. wixWindow.openLightbox(‘schedule’, childRecord) //childRecord having a child_id

  2. my table\collection structure is like this:
    – player collection with team_id and child_id
    – games collection with team_id and game information

  3. I want to populate a table with games (a schedule) information for the child_id

  4. on the ‘Schedule’ page I have a dataset and a table.

My question:
— How do I populate the dataset so it can populate the table all begining with the child_id from the lightbox. I am thinking I need a query that calls two tables (player and games) with an .eq (child_id). Then that result should be binded to a dataset (dataset1) which would populate my tabel (table1’)

This is what I have so far:
wixData.query(“player”)
.eq(“child_id”, child_id)
.include(“Games”)
.find()
.then( (results) => {
if (results.items.length > 0) {
let games = results.items;
let firstGame = games[0];
console.log(firstGame);
//let firstAuthor = firstBook.author;
//let firstAuthorBio = firstAuthor.bio;
//let firstPublisher = firstBook.publisher.name;
} else {
// handle case where no matching items found
console.log(“oh no! found no results”);
}
} )

but I get the following:
Included Games field does not exist in schema. ‘Games’ does have a team_id reference that is also in ‘player’

Hi Luis,

As far as the error that you are receiving, you might check the spelling of the reference field used in the include function. I receive the same error when I intentionally change the spelling of the reference field to the wrong case. Could it be “games” and not “Games”?

Thank you for the response. It is correct. I want to know if the approach I am taking overall is correct or is there a better way to go about it.

You will not be able to query a couple collections and then “bind” the resulting data to a dataset. Instead, you will need to assign the result to the rows property of the table.

It would be helpful if you could post some screen shots of the collections that you are using. You’re attempting something pretty intricate for someone who evidently hasn’t coded much.


it this what you needed?

Luis, I noticed the .NET folder on your Chrome bookmarks bar, and that set me to thinking about your original post comment of “I am thinking I need a query that calls two tables (player and games) with an .eq (child_id).” Unlike the Microsoft “ecosystem”, Corvid does not support writing queries with joins between tables. References are the way of getting at data from other tables in wixData query, as you are already attempting to do.

Your screen shots are helping me see the reason for the “Included Games field does not exist in schema” error. When you use include, it has to be a reference field in the collection that you are querying, not the name of another collection.

Also, spend time getting familiar with the array structure of query results with a console.log(results) line, particularly with how it returns the results of a referenced field. Given what I see of your collections, I think that you had the right idea except that you want to have include function be on season. In the .then section of the code you could then query the Games collection because you would know both the season and team of the child. The results of that query would be the basis of the schedule data that would feed the table. You may need to do some modifications so it displays everything that you want in the table, but you would be most of the way there.

Thank you for the response.

Third paragraph above is causing me issue.
let dataObj = wixWindow.lightbox.getContext();

wixData.query(“player”)
.eq(“child_id”, dataObj.child_id)
.find()
.then( (results) => {
if (results.items.length > 0) {

I first ready children collection to get child_id, use this child_id to call the lightbox. then I read player collection to get team_id, with the team_id I can query Games collection to get the schedule. The above code is returning noting back, goes to an else if it doesn’t return anything.
player collection has: child_id = Wwhat-email@gmail.com, I’m passing Wwhat-email@gmail.com with dataOjb.child_id but get zero results. I need help with this. Once I get this this then I will use the team_id to call Games collections.

Since the field type of the child_id field in the player collection is a reference, have a look at this documentation:

https://www.wix.com/corvid/reference/wix-data.html#isReferenced
https://www.wix.com/corvid/reference/wix-data.WixDataQueryReferencedResult.html

I read the document and made some small modifications. Still getting error: Item [Wwho-email@gmail.com] does not exist in collection [teams]. Tried it for other child_id’s that are referenced and same thing.

  1. Children is going directly to teams

  2. code below
    let dataObj = wixWindow.lightbox.getContext();
    //console.log(dataObj.child_id);
    wixData.queryReferenced(“teams”, dataObj.child_id, “Children”)
    .then( (results) => {
    if (results.items.length > 0) {
    let firstItem = results.items[0]; //see item below
    console.log((“woo hoo found a record”))
    } else {
    // handle case where no matching items found
    console.log(“oh no, no recs found!!!”)
    }
    } )
    . catch ( (error) => {
    let errorMsg = error.message;
    let code = error.code;
    } );

3)screen shots attached. As you can see I the reference seem correct.

The ID that Corvid is expecting in the queryReferenced query is the collection’s built-in ID field called “_id”. Here’s how you get it to show in the collection editor:


TeamsId is the _id value of say the first record in teams.


  wixData.queryReferenced("teams", TeamsId, "child_id")
  .then( (results) => {
    console.log(results);
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

It returns an array like this:


In your query, the records returned would have the children data child_id, teams-2, players_first_name, players_last_name, etc.

Thank you for the code, I’m getting an error: ReferenceError: TeamsId is not defined
I tried different variant ions:
//TeamsId - nope
//“TeamsId”- nope
//“Teamsid” - nope
//Teamsid - nope
//TeamsID - nope
//“TeamsID” - nope
//Teams_ID - nope
//“Teams_ID” - nope
//_id - nope
//“_id” - nope
//“_ID” - nope
//_ID - nope
//_Id - nope
//“_Id” - nope
//teams_id - nope
//“teams_id” - nope
//teamsid - nope
//“teamsid” - nope
//teams_Id - nope
//“teams_ID”- nope
//teams_ID - nope
//teamsId - nope
//“teamsId” - nope
//teamsID - nope
//“teamsID” - nope
//“_id” - nope
//_id - nope

The only way that I get it to work is when I passed the actual value in teams _ID, which I don’t have since I am getting the child_id from the Childrens collection that references a record in teams table:
d7ccd41e-7c07-4eab-9685-b25f85b4e91e

Right, the idea was that TeamsId is the _id value for that record.

You will need to run multiple queries to get the result that you want. That takes patience and persistence to learn how to do. The main thing that you need to be mindful about is that each query takes time to retrieve the data. In the meantime, any lines of code after that in the function will execute and before you want them to, if you’re not careful. Have a look at this article to get an idea of what’s involved:

https://www.wix.com/corvid/forum/corvid-tips-and-updates/promises-promises