Await for query trigger doesnt work

Hi Team,

we try to wait on succesful query.trigger but get a strange behavior:

This codesnippet is triggered by a button click:

 await SQL_GetBatchNumber.trigger().then(data => {
        const Charge = data?.BTNR[0]
        console.log("Charge", Charge)
        artikel.BatchNumbers = [
        {
            ItemCode: AI3_Label_ArtNr_Wert[r].value,
            BatchNumber: Charge,
            Quantity: AI3_NI_AnzahlIst[r].value,
            ExpiryDate: AI3_DP_Ablaufdatum.value,
            ...(AI3_TB_Chargennummer[r].value == "" || AI3_TB_Chargennummer[r].value == null ? null :
            {
                ManufacturerSerialNumber: AI3_TB_Chargennummer[r].value
            })
        }]
      })     

On the first click, we get nothing back. On the second click, we get an object with the result from the previous query, although the result of the query is correct:

It seems that await is useless or doesnt work. But i'm new to JavaScript. Maybe my way to use the promise is wrong?

Greetings,
Hendrik

Hi @Hendrik,

You don't (can't) use await AND .then, they are 2 different ways of handling promises. Personally, I prefer await, so you'd modify your code like this:

const data =  await SQL_GetBatchNumber.trigger()
const Charge = data?.BTNR[0]
console.log("Charge", Charge)
artikel.BatchNumbers = [
{
    ItemCode: AI3_Label_ArtNr_Wert[r].value,
    BatchNumber: Charge,
    Quantity: AI3_NI_AnzahlIst[r].value,
    ExpiryDate: AI3_DP_Ablaufdatum.value,
    ...(AI3_TB_Chargennummer[r].value == "" || AI3_TB_Chargennummer[r].value == null ? null :
    {
        ManufacturerSerialNumber: AI3_TB_Chargennummer[r].value
    })
}]

If you do want to use then, just remove the word await from the first line of code.

@MikeCB thanks for the quick reply!
i understand and changed it.

But it seems that isnt the solution for this problem.
We have found out by trial and error that the object we are forming is quite correct. But when assigning this object to a state via "setValue", this value is not accepted.

I give it out on the console, with this code:

console.log(best); // the original object
console.log(xCreateDeliveryNote.value); //the state before the assigning
xCreateDeliveryNote.setValue(best); // the assignment
console.log(xCreateDeliveryNote.value); //the state after the assigning

And thats the result:

We found this old thread with a similar problem, but can't solve this.

Thanks for support! :slight_smile:

Hi @Hendrik,

setValue returns a promise, so try await xCreateDeliveryNote.setValue(best) and see if that works.

Hi @MikeCB
thanks for that, but unfortunately the same result :confused:

Are you console logging that from the debug menu or in a script? You may need to enable 'Keep variables in sync' for the query if it's the latter.

If it's the former I've got no idea. :man_shrugging:

Thanks for the hint. But we use the console in the debug-window.
The option "Keep variables in sync" is only available for JS query, not for SQL query.

Any other ideas? We cant going on :confused:

Did you run all of those console logs at the same time? It could be that the last one runs before the variable is updated if so. If you open the variable in the debug menu and look at value, is it correct or still the original state?