我不确定这是否正确或有效地完成...我需要查看是否在其他用户关注者中找到了用户/关注...
以下是代码:
var User = new Schema({
firstName: {type: String},
lastName: {type: String},
following: [{ type: ObjectId, ref: 'User' }]
......});
var userId2 = someoneElse;
var userId = req.query.userId;
User.findOne(userId, function(err, results) {
if(err) {
throw err;
} else {
var i, newArrayF1, newArrayF2, biggerArraySize;
newArrayF1 = results.completedChallenges;
newArrayF2 = results.joinedChallenges;
if(newArrayF2.length > newArrayF1.length) {
biggerArraySize = newArrayF2.length;
} else {
biggerArraySize = newArrayF1.length;
}
for(i =0, i < biggerArraySize, i++) {
if(newArrayF1[i].followers.indexOf(userId2)) {
var obj = {mtype: 2};
res.json(obj);
}
if(newArrayF2[i].following.indexOf(userId2)) {
var obj = {mtype: 1};
res.json(obj);
}
}
if(i == biggerArraySize) {
var obj = {mtype: 0};
res.json(obj);
}
}
});
我正在检查用户是否与其他用户有以下/关注者连接并返回与之关联的整数,
答案 0 :(得分:0)
您在抓取用户时可以使用populate
。
User.findOne(userId).populate('following').exec(function (err, user) {
user.following.forEach(function (person) {
// here you have access to each person in the array
}
}