在 mongodb 中更新文档
Updating a document in mongodb
对于学校,我正在 Angular 做一个有特定目标的项目,我们还没有上很多课,所以我还是一个初学者。我的项目快完成了,但我无意中更新了 MongoDB 数据库中的文档。
我的项目是某种社区站点,您可以将其与 Reddit 进行比较。所以也可以给用户的 post 投票。
现在的问题是我无法为我的 post 投票。我收到错误:
this._spell.toJSON is not a function.
对于更新,我使用的是 PUT 调用,但我从未在 class 中看到过,所以我在互联网上搜索并尝试实现它。我对 Insomnia 的后端调用有效,我的 Post 获得了投票。但是当我尝试在我的前端调用 API 时,我得到了这个错误。
我已经浏览了多个网站和 Whosebug,但仍然没有找到解决我的问题的方法。
我的代码
接收事件的post.component代码:
upvote() {
this.post.upvote();
this._dndDataService.upvote(this.post.id, this.post).subscribe();
}
具有 from 和 toJson 的 Post.Model
toJSON() {
return {
title: this._title,
originalPoster: this._originalPoster,
category: this._category,
comments: this._comments.map(i => i.toJSON()), //fixen indien null
spell: this._spell.toJSON(),
dateCreated: this._dateCreated,
votes: this._votes
}
}
static fromJSON(json: any): Post {
const post = new Post(
json.title,
json.originalPoster,
json.category,
json.spell,
json.dateCreated
);
post._id = json._id;
post._votes = json.votes;
post._comments = json.comments.map(Comment.fromJSON); //fixen indien null
return post;
}
我的 dndDataService 调用我的后端,这与创建 posts 的 POST 方法几乎相同。我也使用 map((p:any) : Post => Post.fromJSON(p))
并且在这里我没有得到上面提到的错误。
upvote(id: string, post: Post): Observable<Post> {
const theUrl = `${this._appUrl}/post/${id}/vote`;
return this.http.put(theUrl, post)
.pipe(map((p:any) : Post => Post.fromJSON(p)));
}
现在我的后端调用接收整个 post 并更新我的数据库,我正在使用 router.param 函数来获取具有正确 ID 的 post,这与我一样有效将其用于多种用途:
router.put('/API/post/:id/vote', function (req, res) {
Post.update({
$set: {
votes: req.post}
},
function (err, post) {
if(err)
res.send("Error updating post");
res.json(post);
})
});
我什至让我的老师帮忙更新这段代码,但还是不行。我真的希望你们中的一些人能帮助我。
如果你需要我的全部代码,请说出你需要什么,我会更新我的post。
也可以在以下位置找到我的代码:https://github.com/SFieuws/WebsiteDnd
我已经解决了这个问题,没有将整个 post 发送到我的后端,而是只发送了选票。其次,从 id 中获取我的 post 是错误的,因为我用它调用了我的旧票数,愚蠢的错误。
数据服务
upvoteComment(id: string, points: Number): Observable<Comment> {
const theUrl = `${this._appUrl}/comments/${id}/vote`;
return this.http.patch(theUrl, {points})
.pipe(map((c:any) : Comment => Comment.fromJSON(c)));
}
后端补丁调用
router.patch('/API/post/:post/vote', auth, function (req, res) {
let query = Post.findById(req.params.post).populate("comments").populate("spell");
query.exec(function(err, post){
if(err) res.send("Post not found");
post.votes = req.body.votes;
post.save(function(err, result){
if(err) res.send("Error updating post");
res.send(post);
})
});
});
对于学校,我正在 Angular 做一个有特定目标的项目,我们还没有上很多课,所以我还是一个初学者。我的项目快完成了,但我无意中更新了 MongoDB 数据库中的文档。 我的项目是某种社区站点,您可以将其与 Reddit 进行比较。所以也可以给用户的 post 投票。
现在的问题是我无法为我的 post 投票。我收到错误:
this._spell.toJSON is not a function.
对于更新,我使用的是 PUT 调用,但我从未在 class 中看到过,所以我在互联网上搜索并尝试实现它。我对 Insomnia 的后端调用有效,我的 Post 获得了投票。但是当我尝试在我的前端调用 API 时,我得到了这个错误。
我已经浏览了多个网站和 Whosebug,但仍然没有找到解决我的问题的方法。
我的代码
接收事件的post.component代码:
upvote() {
this.post.upvote();
this._dndDataService.upvote(this.post.id, this.post).subscribe();
}
具有 from 和 toJson 的 Post.Model
toJSON() {
return {
title: this._title,
originalPoster: this._originalPoster,
category: this._category,
comments: this._comments.map(i => i.toJSON()), //fixen indien null
spell: this._spell.toJSON(),
dateCreated: this._dateCreated,
votes: this._votes
}
}
static fromJSON(json: any): Post {
const post = new Post(
json.title,
json.originalPoster,
json.category,
json.spell,
json.dateCreated
);
post._id = json._id;
post._votes = json.votes;
post._comments = json.comments.map(Comment.fromJSON); //fixen indien null
return post;
}
我的 dndDataService 调用我的后端,这与创建 posts 的 POST 方法几乎相同。我也使用 map((p:any) : Post => Post.fromJSON(p))
并且在这里我没有得到上面提到的错误。
upvote(id: string, post: Post): Observable<Post> {
const theUrl = `${this._appUrl}/post/${id}/vote`;
return this.http.put(theUrl, post)
.pipe(map((p:any) : Post => Post.fromJSON(p)));
}
现在我的后端调用接收整个 post 并更新我的数据库,我正在使用 router.param 函数来获取具有正确 ID 的 post,这与我一样有效将其用于多种用途:
router.put('/API/post/:id/vote', function (req, res) {
Post.update({
$set: {
votes: req.post}
},
function (err, post) {
if(err)
res.send("Error updating post");
res.json(post);
})
});
我什至让我的老师帮忙更新这段代码,但还是不行。我真的希望你们中的一些人能帮助我。
如果你需要我的全部代码,请说出你需要什么,我会更新我的post。
也可以在以下位置找到我的代码:https://github.com/SFieuws/WebsiteDnd
我已经解决了这个问题,没有将整个 post 发送到我的后端,而是只发送了选票。其次,从 id 中获取我的 post 是错误的,因为我用它调用了我的旧票数,愚蠢的错误。
数据服务
upvoteComment(id: string, points: Number): Observable<Comment> {
const theUrl = `${this._appUrl}/comments/${id}/vote`;
return this.http.patch(theUrl, {points})
.pipe(map((c:any) : Comment => Comment.fromJSON(c)));
}
后端补丁调用
router.patch('/API/post/:post/vote', auth, function (req, res) {
let query = Post.findById(req.params.post).populate("comments").populate("spell");
query.exec(function(err, post){
if(err) res.send("Post not found");
post.votes = req.body.votes;
post.save(function(err, result){
if(err) res.send("Error updating post");
res.send(post);
})
});
});