【问题标题】:Angular 6: Adding data to database using IDAngular 6:使用 ID 将数据添加到数据库
【发布时间】:2018-06-27 21:55:50
【问题描述】:

我有一个应用程序只显示来自外部 api 的电影,所以我在我的应用程序中添加了单个电影的评论部分,我的问题是当用户在名为 "TITANIC" 的电影中输入评论时 我可以看到添加到所有电影的评论This is wrong,我想要的是添加到电影的评论 TITANIC 不应该显示到其他电影。

这是我所拥有的:

server.js 更新

router.post('/comments', function(req, res) {
  var comment = new Comments(req.body);
  comment.save()
   .then(item => {
   res.status(200).json({'comment': 'comment added successfully'});
   })
   .catch(err => {
   res.status(400).send("unable to save to database");
   });

});

service.js

  // Adds comments
      addReview(author, description): Observable<any> {
        const uri = 'http://localhost:8000/movies/comments/';
        const obj = {
          author: author,
          description: description
        };
        return this.http.post(uri, obj);
      }

  // Return Comments
  getComments(id: string): Observable<any> {
    const url = `${apiUrl + this.commentsUrl}/${id}`;
    return this.http.get(url, httpOptions).pipe(
      map(this.extractData),
      catchError(this.handleError));
  }

组件.ts

addReview(author, description) {
    this.moviesService.addReview(author, description).subscribe(success => {
        this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
        // get the id
        this.activeRouter.params.subscribe((params) => {
          // tslint:disable-next-line:prefer-const
          let id = params['id'];
          this.moviesService.getComments(id)
            .subscribe(comments => {
              console.log(comments);
              this.comments = comments;
            });
        });
    }, error => {
        this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
    });
}

问题

我的代码中缺少什么?

【问题讨论】:

  • 代码看起来不错。请同时发布 getComments 服务方法。
  • 服务方法 addReview 应该做什么?我认为它应该为一部特定的电影添加一条评论,但 URL 的内容不同。我曾想象过类似localhost:8000/movies{movieId}/cmets/
  • @baj9032 现在检查 qn,我已经添加了你从服务中请求 getComments 的方法
  • @addreview 方法我想使用ID将评论添加到特定电影中,现在它将cmets添加到所有电影而不是特定电影,我被困在这里如何解决这个问题,你能帮忙吗?

标签: javascript node.js angular express


【解决方案1】:

您应该 /post 到特定的电影 ID,例如,在其 cmets 字段数组下添加新评论。

另外,使用 /cmets 是一种不好的做法,因为您真正想要的是发布到带有附加评论的特定电影。类似于以下内容:

router.post('/movie/:id/newcomment:', function(req, res) {
  Movie.findOne({ _id: req.params.id })
      .then(movie => {
         movie.comments.push(req.body.comment);
         movie.save();
             .then(result => {
             res.status(200).json({'message': 'comment added successfully'});})
             .catch(err => {
              res.status(400).send("unable to save to database");
            });
        }
});

但这里的主要问题是你误解了你到底需要做什么。

您从现有 API 获取数据,但您希望从获取的数据中将特定 cmets 添加到特定电影。

如果不完全控制您的外部 API,您将无法实现它。

您实际上可以做的是构建一个客户端数据结构来保存所有获取的数据,然后向该数据结构内的每个电影对象添加另一个名为 cmets 的属性和一个 ID 属性,以便您以后可以通过 ID 访问该数据结构并更新特定属性。

【讨论】:

  • 嗨,addReview 的服务方法假设也照顾这些变化?
【解决方案2】:

1) 将对应的电影 id 添加到 POST 请求中添加新评论。

addReview(movieId, author, description): Observable<any> {
        const obj = {movieId, author, description};

2) 当您通过电影 ID 向 cmets 请求电影时,请在数据库查询中使用 movieId(取决于您的数据库和 ORM)

如果你使用的是 mongodb 和 mongoose,它会是这样的:

router.get('/comments/:movieId', function(req, res) {
  const movieId= request.params.movieId;
  Comments.find({ movieId });

【讨论】:

  • 组件呢?
  • 组件调用addReview方法对吧?它只需要传递movieId 作为参数。这是你的问题吗?
  • 是的,我尝试实现你的方法,但它不起作用。
  • 我只是想将评论发布到特定ID,问题从服务器中的cmets方法发布。
  • 有什么问题?如果您使用的是 mongoose,则需要将 movieId 添加到您的评论模式中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
  • 2017-07-14
  • 2015-10-27
  • 1970-01-01
  • 2019-10-19
  • 2018-01-22
  • 2017-01-15
相关资源
最近更新 更多