Search code examples
node.jspostgresqlpg

Update database call in a map node.js pg


I have a node app and I am trying to update multiple rows in the database. I thought I could just use a map function and for each item that needs to be updated do an sql update but I am getting an error:

UnhandledPromiseRejectionWarning: error: syntax error at or near "WHERE" at Parser.parseErrorMessage (/Users/kristen/Documents/classes/momentum/node_modules/pg-protocol/dist/parser.js:278:15)

const updateOrder = (request, response) => {
  const { newOrder } = request.body
  console.log('newOrder !!!!!', newOrder)
  newOrder.map(item => {
    console.log('item', item)
    const orderId = item.order_id
    const id = item.id
    console.log('item', id)
    console.log('orderIdtem', orderId)
    pool.query(
      'UPDATE employees SET order_id = $1, WHERE id = $2',
      [orderId, id]
    )
  })
  response.status(200).send('Order Updated')
}

Solution

  • You have an extra comma before WHERE that causes an error:

    UPDATE employees SET order_id = $1 WHERE id = $2

    should be instead.