5

mysql node docs give an example of how to escape and do neat things. I am unable to figure out how to insert the current time using this approach.

var post  = {id: 1, createdDate: 'NOW()'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
});
// Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: 'NOW()' for column 'createdDate' at row 1
Kaya Toast
  • 5,267
  • 8
  • 35
  • 59
  • 1
    There is issue https://github.com/felixge/node-mysql/issues/701... – Oleksandr T. Jan 01 '15 at 15:48
  • @Alexander Thanks. Using custom formats seems complicated, I was hoping for an easier solution :-) I guess, I'll have to avoid using objects/arrays as data in the `connection.query` and escape them directly as individual variables. – Kaya Toast Jan 01 '15 at 15:54

1 Answers1

6

An easy workaround if you do not want to use custom formats would be to generate date on server-side using new Date() :

var post = {
    id: 1,
    createdDate: new Date()
};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {});

But keep in mind you can have small delay because of server/network lag.

hg8
  • 1,082
  • 2
  • 15
  • 28