Store the result of a query in a variable

I have an insert query

INSERT INTO
  "venture" (
    "venture_name",
    "venture_type"
  )
VALUES
  ({{ input_venture_name.value }}, {{ input_venture_type.value }})
RETURNING venture_id;

In the code panel, I created a variable called new_venture_id and set its initial value to 0.


When the insert runs successfully, I want to assign the value of the returned ID to the variable. I created a success handler on the query

new_venture_id.setValue(insertQuery.data[0].venture_id)

I see the record created successfully in my database, but the variable is still set to 0. Any idea what I'm doing wrong?

Hey there @TBuntel,

Just double checking if you're referencing the query correctly, from the screenshot it seems to be named insertVenture and your setVariable refera to insertQuery

Ah, sorry - screenshots were out of sync. Yes, it's all referring to InsertVenture

1 Like

It looks like it is storing it, but in an object. I can see the value with {{ new_venture_id.value.venture_id }}. Why is it creating the variable as an object and not a number?

1 Like

I think you may want to refer directly to the index, i.e.

new_venture_id.setValue(InsertVenture.data.venture_id[0])

1 Like

Thanks. That did it. The success event handler is

new_venture_id.setValue(InsertVenture.data)

and then I can reference the variable as

{{ new_venture_id.value.venture_id }}

Still find it odd that it created the variable as an object and not as a number. I'd expect to be able to get the value with just

{{new_venture_id}}

anyway...thanks for the feedback!

1 Like