【发布时间】:2018-04-03 09:31:34
【问题描述】:
我有一个 posts 集合,每个帖子都有一个 id 和 userId。
我想查找特定用户的所有帖子(使用 userId 属性)..
这是我的 angular2 服务:
export class PostsService {
private _url = '/api/posts';
constructor(private _http: Http) { }
getPosts(): Observable<Post[]> {
return this._http.get(this._url).map(res => res.json());
}
getUserPosts(userId: number) {
return this._http.get(this._url + '/' + userId).map(posts => posts.json());
}
}
还有我的 nodejs 代码:
router.get('/posts', function(req, res) {
post.find({}).exec(function(err, posts) {
if (err) {
console.log("ERROR FETCHING POSTS!!");
} else {
res.json(posts);
}
});
});
router.get('/posts/:id', function(req, res) {
post.findById(req.params.id).exec(function(err, posts) {
if (err) {
console.log("ERROR FETCHING POSTS!!" + err);
} else {
res.json(posts);
}
});
});
【问题讨论】:
-
你还在纠结这个吗??
-
是的...任何想法):?
-
你能添加你的猫鼬帖子架构吗?
标签: javascript angularjs node.js mongoose mean-stack