我正在尝试按 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/