我试图使用Nodejs mongoose和Mongodb在我的演示项目中添加喜欢和不喜欢的按钮。
每次用户点击喜欢或不喜欢的按钮时都会进行AJAX调用。
每个用户都有一个名为postsReacted的数组,其中包含键为“id”的对象,它是营地的“id”,“反应”是“喜欢”或“不喜欢”,具体取决于他按下的按钮。
req.params.mode是一个“喜欢”或“不喜欢”的字符串,每个阵营都有正在更新的“喜欢”和“不喜欢”的总数。 foundUser.save()回调运行没有问题,但是当我通过mongodb shell检查时它不会更新该用户的“反应”,这只会在执行最后一个else条件时发生,适用于其他情况。
路线是:“/:id / count /:userId /:mode”
req.params.id是营地的mongodb id,req.params.userId是用户的mongodb id,req.params.mode是“喜欢”或“不喜欢”,具体取决于点击的按钮。
露营地型号:
var mongoose = require("mongoose");
// Schema for campgrounds
var campSchema = new mongoose.Schema({
name: String,
price: String,
likes: Number,
dislikes: Number,
image: String,
location: String,
lat: Number,
lng: Number,
description: String,
author: {
id:{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment" // Model's Name
}
]
});
module.exports = mongoose.model("Camp", campSchema);
用户模型:
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var userSchema = new mongoose.Schema({
username: String,
password: String,
postsReacted: []
});
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", userSchema);
路线:
Camps.findById(req.params.id, function(err,foundCamp){
if(err) {
console.log(err);
req.flash("error", "Couldn't do this event, some error occured");
res.redirect("back");
} else{
var interchangable = {"likes":"dislikes", "dislikes":"likes"};
Users.findById(req.params.userId, function(err, foundUser) {
if(err) {
console.log(err);
} else {
var index = foundUser.postsReacted.findIndex(function(post){
return post.id.equals(foundCamp._id);
});
if (index===-1) { // When he hasnt reacted to this camp.
foundUser.postsReacted.push({id: foundCamp._id , reaction: req.params.mode});
foundCamp[req.params.mode]+=1;
} else { // If he has reacted to this camp
if (foundUser.postsReacted[index].reaction === req.params.mode) { // He has clicked the same button of his older reacion so removing his reaction
foundCamp[req.params.mode]-=1;
foundUser.postsReacted.splice(index,1);
}else { // When the user clicked the opposite button so his reaction needs to be changed // When this runs foundUser.save() doesnt seem to work
foundCamp[interchangable[req.params.mode]]-=1;
foundCamp[req.params.mode]+=1;
foundUser.postsReacted[index].reaction=req.params.mode;
}
}
foundCamp.save(function(err) {
if (err) {
console.log(err)
} else {
console.log("Camp Updated");
}
});
foundUser.save(function(err) {
if (err) {
console.log(err)
} else {
console.log("User Updated");
console.log(foundUser.postsReacted);
}
});
console.log({likes: foundCamp.likes, dislikes: foundCamp.dislikes});
res.send(JSON.stringify({likes: foundCamp.likes, dislikes: foundCamp.dislikes}));
}
})
}
})
问题在于下面代码中的foundUser.save()命令,它可以在索引=== - 1条件或者当FoundUser.postsReacted [index] .reaction === req.params.mode条件满足时工作但是当最后一个else运行时,foundUser.save()不会更新数据库中用户的反应,即使回调运行且没有错误。
答案 0 :(得分:0)
所以我能够通过实际创建postsReacted数组的副本然后在foundUser上使用.set()方法来替换旧的postsReacted数组和新更新的数据来解决我的问题。现在就像魅力一样!