Getting triggered query metadata in JS

I've been wrestling with getting query return metadata from a query triggered from JavaScript, something like this:

await bcQuery.trigger( {
  onFailure: function(error) {
    console.log('failure error');
    console.log(error);
  },
  onSuccess: function(queryResults) {
    console.log('success metadata');
    console.log(bcQuery.metadata);
  }
});

The query results come back in an onSuccess clause no problem, but accessing the metadata of the result is a no-go. I've come to think of the JS query environment as a copy of the existing state at time of execution apart from the query results puncturing back through it. Is that an accurate way to think about it?

It seems that if I trigger a second JS query in the onSuccess clause that just prints out the metadata, that works great, so I guess the environment is not a spawn of a copy but a fresh copy of the top-level environment :slight_smile: I can also add a success event handler on the original query (bcQuery in the above example), one that just copies the query metadata into localStorage, and then this Javascript clip can poke into localStorage and pull the metadata from there. But will that sync properly, I mean will the bcQuery success event handler complete before the JS query above continues into onSuccess?

I can't help feeling like I'm just missing some simple way to get the metadata :slight_smile: Is there one and I'm just missing it? Please say yes :slight_smile: Thanks for any thoughts!

EDIT: Oops, I think I'm wrong about localStorage... just re-tried this with both localStorage and temporary state and I think I was just seeing values from previous queries. gah!

Replying to myself... doing a second query trigger to grab the first query's metadata seems to work reliably. It's just gross. I hope there's a better way! :slight_smile:

  await getProjectsPaged.trigger( {
      additionalScope: {
        pagenum: pagenum
      },
      onSuccess: function(queryResult) {
        records = records.concat(queryResult);
      }
  });

  await getProjectsPagedMetadata.trigger( {
      onSuccess: function(queryResult) {
        expectedResults = parseInt(queryResult[0]);
      }
  });

Hey @markdeloura!

You can try turning on this setting for your query:

That should allow you to reference your query's metadata after awaiting it:

await getProjectsPaged.trigger( {
additionalScope: {
pagenum: pagenum
},
onSuccess: function(queryResult) {
records = records.concat(queryResult);
}
});

console.log(getProjectsPages.metadata);

Can you let me know if that works?

1 Like

Hoooooooly smokes. That seems to work great. So much better than my hacky solution. Thank you!
I tried it inside the onSuccess and it didn't work in there (which makes sense, now that I think about it.)
Thanks @Kabirdas!