【问题标题】:How should the data be sent for datatype TIME in Sequelize/ Node JS如何在 Sequelize/Node JS 中为数据类型 TIME 发送数据
【发布时间】:2019-03-27 05:22:10
【问题描述】:

我的 mssql 数据库中有一个时间列。我想通过 API 将数据发布到此表,但此字段引发错误 - “从字符串转换日期和/或时间时转换失败”。我将时间字段作为“13:00”之类的字符串发送。通过sequelize从db获取数据时,我得到一个像“1970-01-01T08:00:00.000Z”这样的ISOString,但是db上的数据像“13:00:00”(hh:mm:ss) .我尝试发送“13:00:00”和“1970-01-01T08:00:00.000Z”,似乎都不起作用。

【问题讨论】:

  • 你能发布你的表结构和你的模型定义吗?

标签: node.js sql-server sequelize.js


【解决方案1】:

我用 sequelize 创建了一个简单的 nodejs 应用程序,并且能够成功地为 Time 数据类型插入 13:0013:00:00

我检查了TimeDate 数据类型。 仅当我尝试将13:00 插入Date 数据类型时,我才收到此错误Conversion failed when converting date and/or time from character string。所以检查你的列的数据类型并确保它是Time 而不是Date

请参考此代码并检查您哪里出错了。

var http = require('http');
const Sequelize = require('sequelize');
const sequelize = new Sequelize('DBName', 'UserName', 'Password', {
  host: 'localhost',  
  port:12672,
  dialect: 'mssql',
  options:{
    encrypt:false,
    instancename: 'SQLEXPRESS'
  }
});



const User2 = sequelize.define('user2', {
  name: {
    type: Sequelize.STRING
  },
  DateTimeCol: {
    type: Sequelize.DATE
  },
  TimeCol: {
    type: Sequelize.TIME
  }
});

User2.sync().then(() => {
   User2.create({
    name: 'Rahul',
    DateTimeCol: '2018-01-01T08:00:00.000Z',
    TimeCol: '13:00'
  });
  User2.findAll().then(data => console.log(data));
});


var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();
});
server.listen(8080);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-10-21
  • 2018-08-18
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2018-12-02
相关资源
最近更新 更多