如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL

How to pass a DateTime from NodeJS Sequelize to MSSQL(如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL)
本文介绍了如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NodeJS 项目,我正在尝试使用 Sequelize 传递一个UpdateDate"字段.我收到错误从字符串转换日期和/或时间时转换失败".我尝试过传递一些不同的东西:

I have a NodeJS project, and I am trying to pass an 'UpdateDate' field using Sequelize. I am receiving the error 'Conversion failed when converting date and/or time from character string'. I have tried passing a few different things:

Date.now()
new Date().toISOString()

都不行.我错过了一些简单的东西吗?我无法更改表上的列定义.据我所知,将诸如 '2016-05-23 10:39:21.000' 之类的字符串传递给 SQL DateTime 字段在 SSMS 中有效,但在使用 Sequelize 和 Node.js 时似乎是一个问题.

Neither work. Am I missing something simple? I cannot change the column definition on the table. As far as I know, passing a string such as '2016-05-23 10:39:21.000' to a SQL DateTime field works in SSMS, but it seems to be an issue when using Sequelize and Node.

谢谢

扎克

推荐答案

这是由 已知的Sequelize 中的问题.解决方案是将 Sequelize 的日期修补为字符串格式实现,如 在此处解释,以便正确处理所有日期.下面是修复错误的代码.

This is caused by a known issue in Sequelize. The solution is to patch Sequelize's date to string format implementation, like explained here, so that all dates are handled properly. Below is the code that fixes the error.

const Sequelize = require('sequelize');

// Override timezone formatting for MSSQL
Sequelize.DATE.prototype._stringify = function _stringify(date, options) {
  return this._applyTimezone(date, options).format('YYYY-MM-DD HH:mm:ss.SSS');
};

这篇关于如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)