Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

javascript - NodeJs insert current Datetime field into MySQL db

I'm trying to insert a DATETIME field into my MySQL db.

var dt = require('moment')().format('YYYY-MM-DD HH:mm:ss');
    pool.query(
      `insert into login(id, id_utente, data_login) values(NULL,?,?)`,
      [results[0].id],
      [dt],
      (error, results, fields) => {
      }
    );

I get this error:

C:UsersigerDesktopprovaREST_API_WITH_MYSQL-master
ode_modulesmysqllibprotocolParser.js:437
  throw err; // Rethrow non-MySQL errors
  ^TypeError: this._callback.apply is not a function

If I try this code, everything comes right:

pool.query(
      `insert into login(id, id_utente, data_login) values(NULL,?,"2021-01-27 00:00:00")`,
      [results[0].id],
      //[dt],
      (error, results, fields) => {
      }
    );

What i'm doing wrong?

question from:https://stackoverflow.com/questions/65922107/nodejs-insert-current-datetime-field-into-mysql-db

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have your parameters in 2 separate arrays. Put your parameters in the same parameter array.

 pool.query(
  `insert into login(id, id_utente, data_login) values(NULL,?,?)`,
  [results[0].id, dt],
  (error, results, fields) => {
  }
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...