node.js 续集更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42414024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:02:20  来源:igfitidea点击:

Sequelize update

node.jssequelize.js

提问by troyz

I'm trying to update a model in Sequelize using the following code:

我正在尝试使用以下代码更新 Sequelize 中的模型:

exports.updateItem = function(item) {
    return new Promise((fulfill, reject) => {
        models.TimesheetItem.update(item,{where: {id: item.id}})
                            .then(fulfill)
                            .catch(console.dir);
     });
};

Where item is the result of doing models.TimeSheetItem.find()

其中 item 是执行 models.TimeSheetItem.find() 的结果

The call never executes the .then and instead passes an empty object to the .catch.

该调用从不执行 .then,而是将一个空对象传递给 .catch。

I've looked over the documentation and it seems that this is the way to update a row, but I can't get it to work. What am I doing wrong?

我查看了文档,似乎这是更新行的方式,但我无法让它工作。我究竟做错了什么?

Thank you!

谢谢!

回答by piotrbienias

According to the documentation the updatemethod takes two parameters - first one is valueswhich will be used to perform the update, and second one is options- so in your case this is the whereclause. If you want to perform an update on a single instance only, you can do it in two ways - use Model.update()method, which can update multiple instances of this model at once matching the whereclause, or perform instance.update()in order to update only the single instance. First option will look like that:

根据文档,该update方法采用两个参数-第一个参数values将用于执行更新,第二个参数是options-因此在您的情况下,这是where子句。如果只想对单个实例执行更新,可以通过两种方式进行 - 使用Model.update()方法,它可以一次更新该模型的多个实例匹配where子句,或者执行instance.update()以仅更新单个实例。第一个选项看起来像这样:

let updateValues = { name: 'changed name' };
models.Model.update(updateValues, { where: { id: 1 } }).then((result) => {
    // here your result is simply an array with number of affected rows
    console.log(result);
    // [ 1 ]
});

The first option is not very useful when you want to update only single instance. So that is why there is a possibility of performing update()on Sequelize model instance

当您只想更新单个实例时,第一个选项不是很有用。所以这就是为什么有可能update()在 Sequelize 模型实例上执行

let updateValues = { name: 'changed name' };
instance.update(updateValues).then((self) => {
    // here self is your instance, but updated
});


In your case, if the itemparameter is a Sequelize model instance (not a plain javascript JSON object), your update function could be like that

在您的情况下,如果item参数是 Sequelize 模型实例(不是普通的 javascript JSON 对象),则您的更新函数可能是这样的

exports.updateItem = function(item){
    return item.update(values).then((self) => {
        return self;
    }).catch(e => {
        console.log(e);
    });
};

However, if the itemis not a sequelize model instance but only a plain object with values you want to update, it could be done in two ways - first is to use Model.update()(just like you did), or second one is to retrieve TimesheetItemwith id = item.idand then perform instance.update()as shown above

但是,如果item不是续集模型实例而只是具有要更新的值的普通对象,则可以通过两种方式完成 - 第一种是使用Model.update()(就像您所做的那样),或者第二种是检索TimesheetItemwithid = item.id然后执行instance.update()如上图

exports.updateItem = function(item){
    models.TimesheetItem.update(item, { where: { id: item.id } }).then((result) => {
        // here result will be [ 1 ], if the id column is unique in your table
        // the problem is that you can't return updated instance, you would have to retrieve it from database once again
        return result;
    }).catch(e => {
        console.log(e);
    });
};

Or the second option with returning instance and performing update on it

或者返回实例并对其执行更新的第二个选项

exports.updateItem = function(item) {
    return models.TimesheetItem.findById(item.id).then((itemInstance) => {
        return itemIstance.update(item).then((self) => {
            return self;
        });
    }).catch(e => {
        console.log(e);
    });
}

The difference is that you do not need to create and return your own Promise- sequelize methods like update()return promises by themselves.

不同之处在于您不需要创建和返回自己的Promise- 像update()返回承诺这样的方法自己的续集。

回答by Samuel Momanyi

{ error: type "where" does not exist}

Just an update to the solution, Sequelize now gives the error above when you include the 'where' option(as shown below). So take it out and it should work perfectly.

只是对解决方案的更新,当您包含“where”选项(如下所示)时,Sequelize 现在会出现上述错误。所以把它拿出来,它应该可以完美地工作。

exports.updateItem = function(item){
    models.TimesheetItem.update(item, { id: item.id  }).then((result) => {
        // here result will be [ 1 ], if the id column is unique in your table
        // the problem is that you can't return updated instance, you would have to retrieve it from database once again
        return result;
    }).catch(e => {
        console.log(e);
    });
};