Node.js 在POST请求后未呈现Listitems
我的应用程序中有评论部分,我正在尝试使用axios.post请求添加新评论 这是(Sequelize,nodeJS):Node.js 在POST请求后未呈现Listitems,node.js,reactjs,sequelize.js,Node.js,Reactjs,Sequelize.js,我的应用程序中有评论部分,我正在尝试使用axios.post请求添加新评论 这是(Sequelize,nodeJS): 让commentArrayNew=[] app.post('/addComment/:id', (req, res) => { commentArrayNew.push(req.body.comment) Posts.update({ comment: commentArrayNew }, {
让commentArrayNew=[]
app.post('/addComment/:id', (req, res) => {
commentArrayNew.push(req.body.comment)
Posts.update({
comment: commentArrayNew
},
{
where: {
id: req.params.id
}
}).then((post) => {
res.send(post)
}).catch((err) => {
console.log("err", err);
});
})
在我的前端,每当我发送请求时,它都会给我状态200,但不会在页面上呈现新项目
这是我的请求
axios.post(`http://localhost:4000/addComment/${id}`, {
comment: commentValue.value
})
.then((res) => {
window.location.reload();
const {comment} : any = res.data
if(commentValue) {
dispatch(actions.addComment({comment}))
}
console.log('--------location', location.state);
})
.catch((err) => {
toast.info("Server error")
console.log('--------err', err);
})
我正在使用useLocation从另一个组件发送状态:
const grabCommentsFromLocation = () => {
if (location.state) {
let commentArray = location.state.post.items.comment
let listItems = commentArray.map((item: Object[]) => {return <p key={uuidv4()}>{item}</p>})
return listItems
}
}
即使注释被保存到数据库中,但项目仍然没有显示,有什么建议吗?您必须使用res.status(200).json(post);相反,res.send(post)您正在执行
window.location.reload
API调用之后,数据不会丢失吗?即使我删除了那一行,我也会得到相同的结果。您是否尝试在添加注释操作中记录action.payload.comment
?更新后的值可用吗?是的,它们可用。setHeader('Content-Type','application/json');试试这个brono错误,结果是一样的,它不会显示在FE sideres.setHeader('Content-Type','application/json');将此添加到res.sendLet之前。
<div>
<p>Comment : </p>
{grabCommentsFromLocation()}
</div>
export const addComment = (payload : any) => {
return {
type: actionTypes.ADD_COMMENT,
payload
}
}
case actionTypes.ADD_COMMENT:
return {
...state,
posts : [
...posts,
{
comment : action.payload.comment,
}
]
}