Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
Node.js PostgreSQL http - Uncaught error on UPDATE query

This is my server.js. I am creating a simple CRUD todo app.

I am making requests from another NodeJS script.

When I make a PUT request I found (by using breakpoints) that I get an uncaught error: source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression. Even though it is just a simple UPDATE query.

Any help is appreciated.

if (method == "PUT" && /\/todos\/(\S+)\/$/.test(url)) {
  console.log("PUT request at", url);
  const urlId = url.match(/\/todos\/(\S+)\/$/);
  console.log(urlId);

  try {
    let jsonData = "";

    req.on("data", chunk => {
      jsonData += chunk;
    });

    req.on("end", async () => {
      const data = JSON.parse(jsonData);
      console.log([String(data["description"]), Number(urlId[1])]);

      const updateTodo = await pool.query(
          "UPDATE todo SET description = '$1' WHERE todo_id = $2 RETURNING *;", 
          [String(data["description"]), Number(urlId[1])]
      );  // ERROR

      console.log(JSON.stringify(updateTodo));

      res.setHeader("Content-Type", "application/json");
      res.write(JSON.stringify(updateTodo["rows"]));
      res.end();
    });

  } catch (err) {
    console.error(err);
    res.end();
  }
}

Database -

CREATE DATABASE learn_restapi

CREATE TABLE todo(
  todo_id SERIAL PRIMARY KEY,
  description VARCHAR(255)
);

One thing I found is a bug report.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Finally, found the bug.

Rather than specifying the types in the description and todo_id in the UPDATE query, just passing them as strings work.

Also get rid of the single quotes from the query.

This is the updated async function:

req.on("end", async () => {
  const data = JSON.parse(jsonData);
  console.log([data["description"], urlId[1]]);

  const updateTodo = await pool.query(
    "UPDATE todo SET description = $1 WHERE todo_id = $2;", 
    [data["description"], urlId[1]]
  );

  res.setHeader("Content-Type", "application/json");
  res.write(JSON.stringify("Todo was updated!"));
  res.end();
});

I also got rid of the RETURNING statement from the query.

This seem to resolve the issue for me.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!