简体   繁体   English

节点 JS:MySQL 在数据库中保存不正确的 DateTime 值

[英]Node JS: MySQL is saving incorrect DateTime value in database

I am trying to save a record into a MySQL database using Node.js.我正在尝试使用 Node.js 将记录保存到 MySQL 数据库中。 Below is the code下面是代码

const mysql = require('mysql2');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');

const prop = PropertiesReader('properties.properties');

const con = mysql.createConnection({
    host: prop.get('server.host'),
    user: prop.get("server.username"),
    password: prop.get("server.password"),
    port: prop.get("server.port"),
    database: prop.get("server.dbname")
});

exports.createJobApplication = (event, context, callback) => {

        context.callbackWaitsForEmptyEventLoop = false;

        const sql = "INSERT INTO job_applications (idjob, iduser, cover_letter, applied_date) VALUES(?,?,?,?)";
        con.query(sql, [jobApplication.idjob, jobApplication.iduser, jobApplication.cover_letter, 1636366620000], function(err, jobApplicationResult) {
                    if (err) {
                        console.log(err.toString());

                        con.rollback(function() {
                            var response = errorCodes.save_failed;
                            callback(null, response);
                        });

                    } else {}

                }

Unfortunatly, I am failing to save the date as required.不幸的是,我没有按要求保存日期。 The above date 1636366620000 really represents 2021-11-08 10:17:00 but in MySQL database what I see is 0000-00-00 00:00:00 .上面的日期1636366620000确实代表2021-11-08 10:17:00但在 MySQL 数据库中,我看到的是0000-00-00 00:00:00

The applied_date field in MySQL is a DateTime field as well. MySQL 中的applied_date字段也是DateTime字段。

In addition to the hardcoded values I tried below JSON, but I get the same result除了我在 JSON 下面尝试的硬编码值之外,我得到了相同的结果

{
   "job_application":{
      "idjob":5,
      "iduser":112,
      "cover_letter":"ABCD EFGH IJK LMNOP QRST UV WXYZ",
      "applied_date":1636366620000
   },
   "milestones":[
      {
         "title":"title",
         "start_date":1636366620000,
         "end_date":1636366620000,
         "cost":250.0,
         "is_deleted":false
      },
      {
         "title":"title two",
         "start_date":1615544220000000,
         "end_date":1615544220000000,
         "cost":250.0,
         "is_deleted":false
      }
   ]
}

How can I fix this issue?我该如何解决这个问题?

I found the issue.我发现了这个问题。 I have forgotted to wrap my DateTime integer with new Date() .我忘记用new Date()包裹我的 DateTime integer 。 So technically, this is how it should be所以从技术上讲,这就是它应该的样子

const sql = "INSERT INTO job_applications (idjob, iduser, cover_letter, applied_date) VALUES(?,?,?,?)";
        con.query(sql, [jobApplication.idjob, jobApplication.iduser, jobApplication.cover_letter, new Date(1636366620000)], function(err, jobApplicationResult)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM