Monday, 15 May 2017

Update query using MySQL and Node.js

I am using Node.js with MySQL. Everything works fine, however I am having issues when using the UPDATE query.

My database table looks like this:

id     | employeeId
54027  | 8241
63708  | 3421
393835 | 3687

To check for external updates, I am sending a request to an API using the following code:

const obj = response.data.map(function(item) {
        return { id: item.entryId.id, employeeId: item.employeeId.id };
});

A console.log of this, returns the following:

[ { id: 54027, employeeId: 8242 },
  { id: 63708, employeeId: 3422 },
  { id: 393835, employeeId: 3688 }
]

So, since my API call has returned data which is different than what is in my DB, I want to issue an UPDATE query to MySQL, using the following code:

const query = connection.query('UPDATE entries SET employeeID = ? WHERE entrieId = ?', obj,
(error, results, fields) => {
    if (error) console.log(error);
    else {
        console.log('Updated!');
    }
});

I get an error saying that I am using the wrong SQL syntax, then I get this at the bottom:

UPDATE entries SET employeeID =id= 54027,employeeId= 8242 WHERE entrieId =id= 63708,employeeId= 3422

I have been following tutorials and other advice found on forums / documentation for the best and correct way to issue the UPDATE, and am getting confused by my result.



via Jim Dover

No comments:

Post a Comment