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



我有一个具有身份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');

如何在此续集查询中返回插入的 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));
})`

最新更新