首页 > 解决方案 > 使用 nodeJS Sequelize 向 postgresql 添加多个值

问题描述

有没有可能向列添加多个值的方法?现在我有一个评论栏,我有这个代码。

app.put('/addComment/:id', (req, res) => {

Posts.update({
        comment: req.body.comment
    },
    {
        where: {
            id: req.params.id
        }
    }).then((post) => {
    res.send(post)
}).catch((err) => {
    console.log("err", err);
});
})

但是,它不是添加另一个值,而是用 new 更新旧值,有什么方法可以解决我的问题吗?

标签: node.jssequelize.js

解决方案


您可以使用 concat 功能sequelize

它将新值附加到列中。

const {fn, col } = models.sequelize

Posts.update({
 members: fn('CONCAT', col("comment"), req.body.comment)
}, {
 where: {
  id: req.params.id
 }
}).then(function() {
 console.log("Value Appended");
});


推荐阅读