【问题标题】:err TypeError: values.map is not a function错误类型错误:values.map 不是函数
【发布时间】:2021-08-14 03:23:13
【问题描述】:

我正在尝试按 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);
    });
})

我正在使用邮递员测试我的服务器,但每当我尝试将更新的数据发送到数据库时,我总是收到此错误 err TypeError: values.map is not a function

请对如何解决此问题有任何建议吗? 在我的评论数组的数据库数据类型中也是text[] 我也尝试将update 更改为create 但没有结果

【问题讨论】:

  • 您请求的内容类型是什么?你能登录 req.body.comment 吗?
  • req.body 工作正常我已经这样做了,我还通过将 req.body.comment 更改为 [req.body.comment] 解决了这个问题
  • 很好,所以请将您的答案发布给有相同问题的人

标签: node.js postgresql sequelize.js


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我通过将req.body.comment 更改为[req.body.comment] 解决了这个问题。

    【讨论】:

      【解决方案3】:

      检查您的 req.body.comment 并将其作为数组发送

      req.body.comment = ['test']
      

      【讨论】:

        猜你喜欢
        • 2018-04-28
        • 2019-02-14
        • 2021-10-02
        • 2021-10-30
        • 2018-11-22
        • 2017-10-12
        • 2020-06-05
        • 2023-03-15
        • 2018-10-21
        相关资源
        最近更新 更多