Having trouble passing parameters to Firestore query via additionalScope to insert document

I've created a shared query in the Query Library (Internal to Firestore DB) which is referenced from my app as "Import from Query Library" resource. I've tried with GUI and raw, here is the query currently, there are 2 variables defined, newFields and version.
let fields = {{ newFields }}
fields.version = {{ version }}
return await db.firestore().collection('reportcustomfields_staging').add(fields);

Upon clicking a button, the click handler runs JS code to trigger the query, passing the variables needed to insert a document, using additionalScope, like this (stateData is a temp variable):
query12.trigger({
additionalScope: {
newFields: stateData.value,
version: version,
},
})

Here is the error I am getting in the console:
query12 failed (0.854s):Bad Request
from query12 response(query12)
:arrow_forward:in query12.trigger()(button1ClickHandler)
newFields: undefined
version: undefined

  1. :arrow_forward:additionalScope: {newFields: Object, version: 2}
    environment: "production"
    :arrow_forward:in button1ClickHandler.trigger()(button1ClickHandler)

I thought this is how we are supposed to pass variables, via additionalScope, but the error shows that the query variables are undefined - how do I get this to work? Thanks

@serena Welcome to the forum!

Try {{newFields.data}} instead?
Also can you show some screenshots and when hovering over any values what they render?
More info needed!

Thanks, here is a screenshot. The values passed to additionalScope are correct (version = 2 etc) while the parameters to the triggered query are undefined. So my question was why the additionalScope values are not passed as the query parameters (because the documentation suggests this is how to do it?)

What does query12 look like? Are {{newFields}} and {{version}} wrapped in curly braces?
Also not sure how you are setting stateData.value...
Can you preview the javascript query (comment out the query12 trigger and additional scope and simply return stateData.value and version to make sure they are being constructed correctly.

query12 looks like this screenshot (the previous screenshot shows the clickhandler code which triggers query12 passing additionalScope - I have returned/logged stateData.value and version and they are definitely constructed properly, please see the debug console output for additionalScope values in the previous screenshot).

So in the definition for query12 I have not put any values into the variables - I assumed that calling query12.trigger with additionalScope variables would pass them, is that incorrect?

You. need to enter {{newFields}} and {{version}} in these fields:
Screenshot 2023-07-26 at 8.47.59 AM

That doesn't work. Those variables are not defined in the query definition.

I want to run a script (javascript code on button click handler) which defines some variables first, (I take my temporary state variable and alter it) before I pass them in as parameters when triggering the query.

By not defined you mean they appear as errors in query12? If so that's normal because the values are defined in the JS query and then passed in additional scope at run time when executing query12. In query12 the empty fields for those parameters should be added as {{newFields}} and {{version}}

Yes, the retool GUI shows Reference Error: newFields is not defined when I put that into the query12 definition. But I did it as you said, and even though what you said makes sense, I still get the same error when I run it (Bad Request). The console debug still shows that additionalScope has newFields defined correctly, but the newFields parameter still shows as undefined.

It's exactly the same as before so it's the same as the previous screenshot of the debug console. Not sure why this doesn't work.

It may be because you are Importing it from the Query Library - try copying query12 and add it directly to the app and see what happens.

(Using the query library means you need to set up the variables being passed into it in the Query Library itself. And then add those variable names to the fields of the query imported.)

OK it is working now, thank you.
I'm not sure why but the shared query in my Query Library which defined the firestore document insertion (which was referenced by query12) looked like it had a variable {{ newFields }} defined, but it was not properly defined?? I had it in raw mode and the variable showed up, but switched to GUI mode and the variable disappeared - so after changing the query to GUI mode and making sure the variable was defined, I tried again (with your fix of adding {{ newFields }} in the query12 definition, and now it works.

I hope I explained that to you properly as it is pretty strange, but thanks for your help!

Just saw your latest reply, I didn't know I could do that, it seems easier not to import from Query Library then..

Happy to hear it!