我想在我的应用程序上添加一个“赞”按钮,但是它不起作用。我没有任何特定的错误消息,只有400(错误请求)。
这就是我所做的:
post.HTML
<button (click)="onStar(post._id,post.like)"> {{ post.like }} fans </button>
post.TS
onStar(id:string,like:number) {
like ++
this.postService.updateLike(id,like).subscribe(
(res) => {
console.log('ok')
}, (error) => {
console.log(error)
}
)
}
post.service.TS
updateLike(id:string, like:number) {
let url = `${this.uri}/like/${id}`;
console.log(like + ' + ' + id)
return this.http.put(url, like, { headers: this.headers }).pipe(
catchError(this.errorMgmt)
)
}
Node.JS控制器:
exports.addLike = (req, res, next) => {
Post.updateOne({ _id: req.params.id }, { like , _id: req.params.id })
.then(() => res.status(201).json({ message: 'Great!'}))
.catch(error => res.status(400).json({ error }));
}
Node.JS路线:
router.put('/like/:id', stuffCtrl.addLike);
非常感谢您的帮助:)祝你今天愉快 !雷切尔
我相信console.log
不是一个承诺。尝试注释掉或删除该行:
Post.updateOne({ _id: req.params.id }, { like , _id: req.params.id })
.then(() => res.status(201).json({ message: 'Great!'}))
.catch(error => res.status(400).json({ error }));