【发布时间】: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