0
我在尝试更新猫鼬模式。基本上我有两个API'/ follow /:user_id'和'/ unfollow /:user_id'。我想要实现的是每当用户A跟随用户B时,用户B在猫鼬中的追随者字段将作为一个增量。同时更新猫鼬模式字段
至于现在我设法只得到以下字段增加一,但不是追随者领域。
schema.js
var UserSchema = new Schema({
name: String,
username: { type: String, required: true, index: { unique: true }},
password: { type: String, required: true, select: false },
followers: [{ type: Schema.Types.ObjectId, ref: 'User'}],
following: [{ type: Schema.Types.ObjectId, ref: 'User'}],
followersCount: Number,
followingCount: Number
});
更新版本:我想我的解决方案,但每当我张贴,它只是获取数据(我试过邮差Chrome应用API的)。
api.js
// follow a user
apiRouter.post('/follow/:user_id', function(req, res) {
// find a current user that has logged in
User.update(
{
_id: req.decoded.id,
following: { $ne: req.params.user_id }
},
{
$push: { following: req.params.user_id},
$inc: { followingCount: 1}
},
function(err) {
if (err) {
res.send(err);
return;
}
User.update(
{
_id: req.params.user_id,
followers: { $ne: req.decoded.id }
},
{
$push: { followers: req.decoded.id },
$inc: { followersCount: 1}
}
), function(err) {
if(err) return res.send(err);
res.json({ message: "Successfully Followed!" });
}
});
});
这些代码只设法增加用户的以下字段,并没有重复。如何在字段以及其他用户的关注者字段中同时更新登录用户的?
更新的版本:它不断提取数据。
谢谢你的帮助,它不起作用。它只更新以下和以下计数字段,但不关注追随者和追随者计数。 – sinusGob 2015-02-11 13:53:30
检查更新后的版本,我附上了一张图片 – sinusGob 2015-02-11 13:56:55
当你尝试'console.log(user)'时,你得到了什么? – Khay 2015-02-11 14:17:35