从req.body获取未定义

时间:2018-09-05 17:07:30

标签: node.js mongodb reactjs express

当我调度startAddLike时,我在服务器端即uid中获得了不确定的uid值。 req.body.uid = undefined。如何解决?

动作生成器:

export const startAddLike = (id) => {
    return (dispatch, getState) => {
        const uid = getState().auth.uid;
        axios
            .post(`http://localhost:5000/api/posts/${id}/like`, uid)
            .then(res => dispatch(likePost(id, uid)))
    }
}

类似邮政路线

router.post('/:id/like', (req, res) => {
    console.log(req.body.uid);
    Post.findOneAndUpdate({_id:req.params.id}, {$push: {likes: req.body.uid}},{ "new": true})
        .then(post => {
            console.log('l'+post);
            res.json(post);
        })
});

1 个答案:

答案 0 :(得分:0)

在这里,uid应该作为对象传递。

所以应该是

export const startAddLike = (id) => {
    return (dispatch, getState) => {
        const uid = getState().auth.uid;
        axios
            .post(`http://localhost:5000/api/posts/${id}/like`, { uid: uid })
            .then(res => dispatch(likePost(id, uid)))
    }
}

希望可以解决您的问题。