sql-server - 如何使用 SequelizeJS 从表中返回非常插入的 ID?

标签 sql-server node.js sequelize.js

我有一个带有身份 ID 的表。每当将新记录插入给定表时,此值都会自动增加。但是,我无法返回插入的 ID 以响应我的查询。

module.exports = sequelize.define('MyTable', {
    aField1: Sequelize.STRING,
    aField2: Sequelize.STRING,
    id: { type: Sequelize.BIGINT, primaryKey: true, autoIncrement: true }
})

通过调用 create() 函数将项目正确插入到数据库中,但执行的查询没有返回任何 ID。日志告诉我以下内容:
Executing (fdfd7f79d028c57c1c6f): 
INSERT INTO [dbo].[MyTable] ([aField1], [aField2]) 
OUTPUT INSERTED.* VALUES ('aValue1', 'aValue2');

如何在此 Sequelize 查询中返回插入的 ID?

谢谢您的帮助!

最佳答案

据我所知,您只想发送 id 作为响应。
代码应该看起来像

`.post((req, res, next) => {
    DATABASE_NAME.create(req.body)
    .then((result) => {
        //console.log(result);
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        res.json(result.id);
    }, (err) => next(err))
    .catch((err) => next(err));
})`

关于sql-server - 如何使用 SequelizeJS 从表中返回非常插入的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169314/

相关文章:

json - SQL Server sp_OAMethod 在调用 WebAPI 时返回 NULL

sql-server - SQL - CASE 语句中的别名

node.js - Redis Cloud 用于 Node heroku 应用程序中的 session

node.js - mongodb 聚合中的 if else if cond

javascript - 使用 claudia-api-builder 时如何返回 API 响应?

json - NodeJs Sequelize 删除 toJSON 方法中的属性会导致从模型本身中删除值

sql - 使用大型 Excel 文件插入/更新 SQL

c# - "Streaming"从 SQL Server 中的表读取超过 1000 万行

javascript - 为什么 `{} + 1` 在 Chrome 和 Firefox 中获得数字 1,但在 Node.js 中获得字符串 '[object Object]1'?

javascript - 按日期对 sequelize.js 查询进行排序