Search code examples
javascriptsqldatabaseazurenode-red

How to insert a parameter in the SQL query?


I'm creating a hangout bot using nodered.I have a SQL database on Azure and for each user I have an id saved in msg.conversationId and I would like to pass this id to a query. I create a function "sql query" with this query:

msg.payload = {action: "Q", query: `SELECT a = ISNULL(Nome, null) FROM Users WHERE id_conversation=${msg.conversationId};`};

But, of course I am wrong

This is the node red flow:

Nodered flow

Does anyone has any suggestions? Thanks so much!


Solution

  • To debug your script insert

    node.error(msg.payload);
    

    after your msg.payload definition to output the data in the debug tab.

    Try to concate the string like

    msg.payload = {action: "Q", query: "SELECT a = ISNULL(Nome, null) FROM Users WHERE id_conversation=" + msg.conversationId + ";"};
    

    alternative:

    var query = "SELECT a = ISNULL(Nome, null) FROM Users WHERE id_conversation=" + msg.conversationId + ";";
    msg.payload = {action: "Q", query: query};