每次点击(角度/节点)上的增量数据(MongoDB)作为Facebook的“赞按钮”-不起作用

问题描述 投票:0回答:1

我想在我的应用程序上添加一个“赞”按钮,但是它不起作用。我没有任何特定的错误消息,只有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);

非常感谢您的帮助:)祝你今天愉快 !雷切尔

node.js angular mongodb express mean-stack
1个回答
0
投票

我相信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 }));
© www.soinside.com 2019 - 2024. All rights reserved.