I'm trying to find post by id and add comment to it .
I'm using postgreSQL.
Here is my model
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;
Here is my nodeJS Request.
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);
});
})
I'm testing my server using postman but whenever i try to send updated data to the databse I'm always getting this error err TypeError: values.map is not a function.
Any suggestions on how to fix this please?
Also in my DB datatype for comment array is text[]
I also tried to change update to create but with no results
you have defined comment in the model of type Array of strings but while update you are directly passing the single string please pass in the array format
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);
});
I fixed the issue by changing req.body.comment to the [req.body.comment].
check your req.body.comment and sent it as array
req.body.comment = ['test']