Search code examples
javascriptmysqlnode.js

mysql timestamp now() is not working with nodejs


I am trying to insert a row into mysql table using nodejs. I am trying to update a column creation_date(timestamp) by sending its value as now() in nodejs, but it is giving an error saying that "now" is not defined. When I am executing the same sql query in mysql directly, it is working. I do not know where the problem is. Can somebody please help me here... This is the nodejs code where I am inserting values into the table in mysql..

var sql = `INSERT INTO sales
    (
        user_id, name, tagline, start_date, 
        start_time, end_date, reg_start, 
        reg_end, descr, creation_date
    ) VALUES (
        ?, ?, ?, ?,
        ?, ?, ?,
        ?, ?, ?
    )`

pool.query(sql, [
    id, req.body.sale_name, req.body.tag, req.body.start,
    req.body.stime, req.body.end, req.body.reg_start,
    req.body.start, req.body.descr, now()
], function (err, result) { //further code})

The error which I am getting is : ReferenceError: now is not defined


Solution

  • You can get today's date by using general JavaScript object.

    var datetime = new Date();
    

    So, I guess you should write smth like this:

    pool.query(sql, [
        id, req.body.sale_name, req.body.tag, req.body.start,
        req.body.stime, req.body.end, req.body.reg_start,
        req.body.start, req.body.descr, new Date()
    ], function (err, result) { //further code})