【发布时间】: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