首页 > 解决方案 > 角度 http 帖子从不调用后端操作

问题描述

我正在尝试向我的节点后端执行 http 发布。Todo 所以我尝试了这种方法(通过单击材料底页中的按钮触发)

 return this.http.post<any>('http://localhost:5000/location/addreview', JSON.stringify({
  username: this.data.username,
  ts: Date.now(),
  userid: this.data.userid,
  rate: this.currentRate,
  message: this.message,
  restId: this.data.locId
})
);

这是我的后端:

router.post('/addreview', async(req, res) => {
console.log("called")

    const username = req.body.username;
    const userid = req.body.userid;
    const ts = req.body.ts;
    const rate = req.body.rate;
    const message = req.body.message;
    const restaurantId = req.body.restId;
    collection.update(
        { _id: ObjectId(restaurantId) },
        { $push: { reviews: {   username: username,
                                rate: rate,
                                ts: ts,
                                userid: userid,
                                message: message } } }

     )
     res.end("Review saved");
     res.status(200);
}

})

被调用的打印从未被调用,所以我认为该帖子从未被执行

标签: angularhttpclient

解决方案


推荐阅读