最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用sequelize(mysql)+ nodejs更新记录

运维笔记admin9浏览0评论

使用sequelize(mysql)+ nodejs更新记录

使用sequelize(mysql)+ nodejs更新记录

我的桌子名字:TIME

我的表数据字段:

开始:10.00am停止:null

现在我想将null更新为当前日期

exports.updateGroup = function (req, res) {
    Time.update(req.body, {
        stop: new Date(),
        where: {
            id: req.body.id
        }
    }).then(data => {
        return res.status(200).send(data);
    }).catch(Sequelize.ValidationError, err => {
        return res.status(422).send(err.errors[0].message);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

现在它在更新后显示为null

我的要求:{mainId:1,开始:'',停止:''}

my Time model :

 var Time = sequelize.define('time', {
             start: Sequelize.DATE,
             stop: Sequelize.DATE,

     });
回答如下:

首先,您传递了错误的ID,因为没有基于您发布的正文格式的req.body.id属性。

其次,update方法获得official sequelize documentation引用的两个参数。

这应该工作:

Time.update({
    start: req.body.start,
    stop: req.body.stop
}, {
    where: {
        id: req.body.mainId
    }
})
发布评论

评论列表(0)

  1. 暂无评论