node.js - POST 请求 ReactJS 后列表项未呈现

标签 node.js reactjs sequelize.js

我的应用程序中有评论部分,我正在尝试使用 axios.post 请求添加新评论。
这是 (Sequelize,nodeJS) :let 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,但它没有在页面上呈现新项目。
这是我的 FE 请求
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
    }
}
这是我渲染我的项目的地方:
<div>
                    <p>Comment : </p>
                    {grabCommentsFromLocation()}
                </div>
这是我来自 redux 的 dispatch 函数:
export const addComment = (payload : any) => {
return {
    type: actionTypes.ADD_COMMENT,
    payload
}
}

 case actionTypes.ADD_COMMENT:
        return {
            ...state,
            posts : [
                ...posts,
                {
                    comment : action.payload.comment,
                }
            ]

        }
即使评论保存到数据库中,项目仍然没有显示,有什么建议吗?

最佳答案

你必须使用 res.status(200).json(post);相反 res.send(post)

关于node.js - POST 请求 ReactJS 后列表项未呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62773706/

相关文章:

node.js - 考虑背压,将数据从 Cassandra 流式传输到文件

node.js - 执行升 sails 时 orm 加载失败

node.js - Puppeteer:Element.hover() 不存在

node.js - Google云平台中的Websocket

javascript - Sequelize 包含范围为 1 :m constraint = false 的对象

javascript - 当 Prop 改变时渲染 react 组件

javascript - 通过 props 将状态值传递给子组件

javascript - 在 React JS 中处理 If 条件中的状态值

mysql - 在 Sequelize 中动态定义数据库以支持 Multi-Tenancy 返回错误的查询语法

javascript - 建立关联时,哪些方法/mixin sequelize 添加到模型中?