node.js - 错误类型错误 : values. 映射不是函数

标签 node.js postgresql sequelize.js

我正在尝试按 id 查找帖子并向其添加评论。
我正在使用 postgreSQL。
这是我的模型

   const Sequelize = require('sequelize')
const db = require('../../config/database')

const Posts =db.define("Topics",{
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    title : {
        type : Sequelize.STRING,
    },
    description : {
        type: Sequelize.STRING
    },
    username : {
        type: Sequelize.STRING
    },
    comment : {
        type:Sequelize.ARRAY(Sequelize.STRING),
        defaultValue : [],
        allowNull: false
    }

})
module.exports = Posts;
这是我的 nodeJS 请求。
    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);
    });
})
我正在使用 postman 测试我的服务器,但是每当我尝试将更新的数据发送到数据库时,我总是收到此错误 err TypeError: values.map is not a function
关于如何解决这个问题的任何建议?
同样在我的数据库数据类型中,注释数组是 text[]我也尝试将 update 更改为 create 但没有结果

最佳答案

您已经在字符串数组类型的模型中定义了注释,但是在更新时您直接传递单个字符串,请传入数组格式

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

关于node.js - 错误类型错误 : values. 映射不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62751501/

相关文章:

sequelize.js - Sequelize - 带有前缀表名的别名属性

MySQL:如何忽略该列不明确

mysql - Sequelize GROUP BY 仅在主表而不是连接中的完整连接表上聚合

javascript - 在 Elasticsearch 中查询

javascript - JS - 如何使用链式 promise 结束数据库连接

mysql - Rails 更改 MySQL 和 Postgres 的列类型迁移

postgresql - 在 CL 后现代什么 :col-type to use for Many to Many field?

postgresql - 在 postgresql 函数中有条件地加入输入值

node.js - npm 脚本的时间/性能指标

node.js - 在不同于 .net 的 Node 中创建的安全 token