【问题标题】:My database in not updating in Sequelize我的数据库未在 Sequelize 中更新
【发布时间】:2018-04-25 13:50:00
【问题描述】:

我正在尝试使用此代码来更新我的值,但是当我 res.json 时,它给了我 [0] 任何可以帮助我解决这个问题的人。提前致谢。

          var condition={
                where:
                {id:req.params.id}
            };
            var values={
                title:req.body.title,
                slug:req.body.slug,
                content:req.body.content
            };
            options={multi:true};
            var errors = req.validationErrors();

            if (errors) {
                res.render('admin/edit_page', {
                    errors: errors,
                    title: title,
                    slug: slug,
                    content: content,
                    id:id
                });
            } else {
                // models.Page.find({
                //     where: {
                //         id:req.params.id
                //     }         
                // })
                // .then(function(page){
                //     if(Page){
                    models.Page.update(values,condition,options)

                    .then(function(page){
                        res.render('admin/pages/edit-page');
                    })
                    .catch(function(err){
                        console.log(err);
                    })
                }
                });

【问题讨论】:

  • 在当前代码中,您在 Model 而不是 object 上运行更新。所以如果你有一个类方法update,请在这里显示。您在其中找到一个对象然后运行更新的注释代码,它应该可以工作,除了一个错字,我应该在 page 而不是 Pageif 块内。如果没有,请分享您看到的错误

标签: node.js sequelize.js


【解决方案1】:

根据 doc 更新仅接受 2 个参数,并且在您的函数中发送 3 个

改变这个:

models.Page.update(values,condition,options)

收件人:

models.Page.update(values,{ ...condition, ...options }) // ES6 sprade operator

【讨论】:

  • 现在我收到此错误我已将行更改为此 models.Page.update(values,{ condition, options}) AssertionError [ERR_ASSERTION]: Missing where attribute in the options parameter
  • 嗨,我认为你必须先做两件事。首先在您的数据库中执行行查询并检查更新操作是否正常工作。例如:更新 Page_table set title='a', slug='b', content ='c' where id=1。其次,请记录您的所有请求。身体和要求。参数值并检查其中的任何内容变为空或空。您能否评论一下 req.param.id,req。身体。标题等值在这里?
猜你喜欢
  • 2017-12-16
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多