5

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

6
  • few questions :: is your client and db server running in same timezone? if not which datetime you want to store in db? if you want to store the db time you can ignore this feild in query use NOW() as default value in sql. Commented Dec 20, 2017 at 9:54
  • You have to use 'NOW()' . A silly mistake i guess. Commented Dec 20, 2017 at 9:55
  • Yes it is working in the same timezone Commented Dec 20, 2017 at 9:56
  • you are using node js so use new Date() instead now().now() works when you insert data using mysql Commented Dec 20, 2017 at 10:00
  • Have you tried above? Commented Dec 20, 2017 at 10:04

5 Answers 5

5

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Date() returns the server's time. You should use Date.now(), it returns the UTC time github.com/drudge/mongoose-timestamp/issues/23#issue-51846618
1

The reason you're getting this error because now() works on MYSQL where running now() in javascript looking for function which doesn't exist in your script.

Correct way of loading date in Javascript is "Date.now();"

Reference: JavaScript now() Method

Comments

1

TIMESTAMP and DATETIME columns can be automatically initialized and updated to the current date and time (that is, the current timestamp) in MySQL.

So, here you can set current timestamp without passing it from the client side. Whenever a row is inserted in the MySQL database then the current timestamp will automatically set to the TIMESTAMP/ DATETIME column.

CREATE TABLE Statement:

CREATE TABLE t1 (
  // rest of the statements
  'creation_timestamp' TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  'creation_datetime' DATETIME DEFAULT CURRENT_TIMESTAMP,
);

Now, whenever a row is inserted into the database then the creation_timestamp/creation_datetime column will be automatically set to the current timestamp.

Note:

  1. Here, you don't need to pass any date or timestamp in the insert statement from the client side.
  2. You can learn more about it here.

Comments

0

I think, you have to give now() as a String like that:

  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})

2 Comments

The row got inserted into the table but the value was 00-00-0000 00:00:00 got saved in the table.
@MeghaVerma that's because it's not using NOW() it's using 'NOW()'. I'm surprised it converted the string into a date and didn't just error out.`
0

It is possible to insert a function into a data array for bulk insertion or other type of parameterized queries.

For this, the "raw" function is used. See the example where the data array is iterated and the "NOW()" function is added using the mysql.raw() function. Hope it helps!

var mysql      = require('mysql');

let connection = doConection(); // make conection

let masiveInsertDataArray = rowsDataArray.map(function(row){ 
    let timestamp = new Date(row.timestamp);
    return [
        row.name,                   // "Leonardo"
        row.otherData,              // "fullStackDev"
        row.someRelationalId,       // "18"
        mysql.raw('NOW()'),  // timestamp mysql field created_at
        mysql.raw('NOW()'),  // timestamp mysql field updated_at
    ]; 
});

let sqlStatment = `
    INSERT INTO some_table 
        (name, profession, some_relational_id, created_at, updated_at) 
    VALUES 
        ?
`;

connection.query(sqlStatement, masiveInsertDataArray, (error, result, fields)=>{
    console.log(result);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.