Home > Software engineering >  Is this way of writing UPDATE query is false? [SOLVED]
Is this way of writing UPDATE query is false? [SOLVED]

Time:12-05

I'm updating my postgres database with this query :

await client.connect();
        const statusDeconnect = product.fields.StatusSendDeconnect;
        const UserDeconnect = product.fields.UserDeconnect;
        console.log('Status to add : '   statusDeconnect   ' UserId that will deconnect : '   UserDeconnect);
        const updateSend = await client.queryArray`UPDATE userconnectionstatus SET status = ${statusDeconnect} WHERE idconnection = ${UserDeconnect}`;
        console.log('User '   UserDeconnect   ' Has been deconnected');

i get a postgres error that says idconnection column doesn't exist

result in the console :

enter image description here

Pgadmin page of my table :

enter image description here

CodePudding user response:

It looks like you are trying to use a template string to build a dynamic SQL query in your code. The ${statusDeconnect} and ${UserDeconnect} expressions in your query are placeholders for values that you want to bind to the query at runtime.

However, it appears that you are not providing the actual values for these placeholders when you execute the query. This will cause the query to fail because the placeholders will not be replaced with the values you intended.

To fix this issue, you need to provide the values for the statusDeconnect and UserDeconnect placeholders when you execute the query. This can be done by passing the values as additional arguments to the client.queryArray function, like this:

const updateSend = await client.queryArray(`
    UPDATE userconnectionstatus
    SET status = ${statusDeconnect}
    WHERE idconnection = ${UserDeconnect}
`,
statusDeconnect,
UserDeconnect
);

In this example, the statusDeconnect and UserDeconnect variables are passed as additional arguments to the client.queryArray function, and their values will be used to replace the corresponding placeholders in the query.

I hope this helps! Let me know if you have any other questions.

CodePudding user response:

You're missing brackets around your queryArray function. It should be:

EDIT: As pointed out in the comments, I'm not right - brackets are not required.

const updateSend = await client.queryArray(`UPDATE userconnectionstatus SET status = ${statusDeconnect} WHERE idconnection = ${UserDeconnect}`);
  • Related