请看一下,让我知道为什么循环的输出不正确?
基本上我循环遍历用户的friendId数组并通过用户搜索结果并查看它们是否匹配,具体取决于匹配它应返回true还是false。
这是我的循环代码:
User.findById(req.signedCookies.userid, function(err, signedInUser) {
//console.log(JSON.stringify(signedInUser.friendRequest));
for (var x = 0; x < users.length; x++) {
users[x].isFriend = false;
//console.log(users[x].lastName);
for (var i = 0; i < signedInUser.friendRequest.length; i++) {
// console.log(users[x]._id + ' - ' + signedInUser.friendRequest[i].friendId);
//console.log(users[x].isFriend);
if (users[x]._id === signedInUser.friendRequest[i].friendId) {
users[x].isFriend = true;
console.log('test');
break;
}
}
}
res.render('searchResults', {
title: 'Weblio',
userAdded: users
});
});
console.log的输出:
[{"friendId":"51ee2017c2023cc816000002","read":0,"date_requested":"2013-07-23T06
:29:39.021Z"},{"friendId":"51ee203cc2023cc816000003","read":0,"date_requested":"
2013-07-23T06:42:37.872Z"}]
Jones
51ee2017c2023cc816000002 - 51ee2017c2023cc816000002
false
51ee2017c2023cc816000002 - 51ee203cc2023cc816000003
false
Macks
51ee203cc2023cc816000003 - 51ee2017c2023cc816000002
false
51ee203cc2023cc816000003 - 51ee203cc2023cc816000003
false
签约用户是John Smith,他搜索了Jake
用户: John Smith id在01结束 杰克琼斯在02结束 Jake Macks在03年结束
Jake Macks实际上在朋友家里
console.log('test');
没有被输出,所以我假设它甚至没有进入嵌套循环的if语句
以下是我在您移动的控制台日志之前调用的这些控制台日志的输入:
console.log(users);
console.log(signedInUser);
console.log(users[x].isFriend);
结果是:
[ { firstName: 'Jake',
lastName: 'Jones',
email: 'test2@gmail.com',
password: '$2a$10$3ndDWiqOsyN.WN19fKJqq.xiC0B9da7QKTL74995zCT8vHrClo2uW',
phone: 98439843943,
birthday: Mon Jun 04 2012 20:00:00 GMT-0400 (Eastern Daylight Time),
_id: 51ee2017c2023cc816000002,
__v: 0,
friend: [],
friendRequest: [] },
{ firstName: 'Jake',
lastName: 'Macks',
email: 'test3@gmail.com',
password: '$2a$10$XTsGrWmmOH/3O3eNwrNK2u.XOwl5cPPGyKrzgU0RMROcGTtU1LkDK',
phone: 49372432922,
birthday: Mon Jun 04 2012 20:00:00 GMT-0400 (Eastern Daylight Time),
_id: 51ee203cc2023cc816000003,
__v: 0,
friend: [],
friendRequest: [] } ]
{ __v: 0,
_id: 51ee1ddbc2023cc816000001,
birthday: Mon Aug 06 2012 20:00:00 GMT-0400 (Eastern Daylight Time),
email: 'test1@gmail.com',
firstName: 'John',
lastName: 'Smith',
password: '$2a$10$w6jTLvW.gUW5tY59/2/vIu8XPVsOe/NTr3e.Qc0WvVKIG8/MwSDW.',
phone: 1122334422,
friend: [],
friendRequest:
[ { date_requested: Tue Jul 23 2013 02:29:39 GMT-0400 (Eastern Daylight Time)
,
read: 0,
friendId: 51ee2017c2023cc816000002 },
{ date_requested: Tue Jul 23 2013 02:42:37 GMT-0400 (Eastern Daylight Time)
,
read: 0,
friendId: 51ee203cc2023cc816000003 } ] }
false
[ { firstName: 'Jake',
lastName: 'Jones',
email: 'test2@gmail.com',
password: '$2a$10$3ndDWiqOsyN.WN19fKJqq.xiC0B9da7QKTL74995zCT8vHrClo2uW',
phone: 98439843943,
birthday: Mon Jun 04 2012 20:00:00 GMT-0400 (Eastern Daylight Time),
_id: 51ee2017c2023cc816000002,
__v: 0,
friend: [],
friendRequest: [] },
{ firstName: 'Jake',
lastName: 'Macks',
email: 'test3@gmail.com',
password: '$2a$10$XTsGrWmmOH/3O3eNwrNK2u.XOwl5cPPGyKrzgU0RMROcGTtU1LkDK',
phone: 49372432922,
birthday: Mon Jun 04 2012 20:00:00 GMT-0400 (Eastern Daylight Time),
_id: 51ee203cc2023cc816000003,
__v: 0,
friend: [],
friendRequest: [] } ]
{ __v: 0,
_id: 51ee1ddbc2023cc816000001,
birthday: Mon Aug 06 2012 20:00:00 GMT-0400 (Eastern Daylight Time),
email: 'test1@gmail.com',
firstName: 'John',
lastName: 'Smith',
password: '$2a$10$w6jTLvW.gUW5tY59/2/vIu8XPVsOe/NTr3e.Qc0WvVKIG8/MwSDW.',
phone: 1122334422,
friend: [],
friendRequest:
[ { date_requested: Tue Jul 23 2013 02:29:39 GMT-0400 (Eastern Daylight Time)
,
read: 0,
friendId: 51ee2017c2023cc816000002 },
{ date_requested: Tue Jul 23 2013 02:42:37 GMT-0400 (Eastern Daylight Time)
,
read: 0,
friendId: 51ee203cc2023cc816000003 } ] }
false
答案 0 :(得分:4)
您遇到的一个问题是您尝试记录isFriend结果的位置。在您进入搜索循环时将isFriend设置为false后,它正在被记录。
该语句需要在内部for循环之后移动。
如果没有所有输入,就很难猜出问题的位置。
使用以下作为输入(你能提供JSON格式的实际输入吗?):
var users = [
{
lastName: 'Smith',
'_id': "51ee2017c2023cc816000001"
},
{
lastName: 'Jones',
'_id': "51ee2017c2023cc816000002"
},
{
lastName: 'Macks',
'_id': "51ee2017c2023cc816000003"
}
];
var signedInUser = {
friendRequest: [{
"friendId": "51ee2017c2023cc816000002",
"read": 0,
"date_requested": "2013-07-23T06:29:39.021Z"
}, {
"friendId": "51ee203cc2023cc816000003",
"read": 0,
"date_requested": "2013-07-23T06:42:37.872Z"
}]
};
function test(err, signedInUser) {
console.log("\nsignedInUser.friendRequest\n" + JSON.stringify(signedInUser.friendRequest, null, 2));
for (var x = 0; x < users.length; x++) {
users[x].isFriend = false;
console.log("\n" + users[x].lastName);
for (var i = 0; i < signedInUser.friendRequest.length; i++) {
console.log(users[x]._id + ' - ' + signedInUser.friendRequest[i].friendId);
if (users[x]._id === signedInUser.friendRequest[i].friendId) {
users[x].isFriend = true;
console.log('test');
break;
}
}
console.log(users[x].isFriend);
}
console.log("\nFinal users:\n" + JSON.stringify(users, null, 2));
}
test(null, signedInUser);
我看到以下结果:
signedInUser.friendRequest
[
{
"friendId": "51ee2017c2023cc816000002",
"read": 0,
"date_requested": "2013-07-23T06:29:39.021Z"
},
{
"friendId": "51ee203cc2023cc816000003",
"read": 0,
"date_requested": "2013-07-23T06:42:37.872Z"
}
]
Smith
51ee2017c2023cc816000001 - 51ee2017c2023cc816000002
51ee2017c2023cc816000001 - 51ee203cc2023cc816000003
false
Jones
51ee2017c2023cc816000002 - 51ee2017c2023cc816000002
test
true
Macks
51ee2017c2023cc816000003 - 51ee2017c2023cc816000002
51ee2017c2023cc816000003 - 51ee203cc2023cc816000003
false
Final users:
[
{
"lastName": "Smith",
"_id": "51ee2017c2023cc816000001",
"isFriend": false
},
{
"lastName": "Jones",
"_id": "51ee2017c2023cc816000002",
"isFriend": true
},
{
"lastName": "Macks",
"_id": "51ee2017c2023cc816000003",
"isFriend": false
}
]
除了日志语句在错误的地方(我认为我没有改变你的代码的语义),使用这组输入,逻辑工作。您期望的输入很可能不是您所接受的。
事实证明OP正在使用用于nodejs的mongoose本机驱动程序,并且在研究之后找到了问题的比较部分的答案:Comparing mongoose _id and strings
答案 1 :(得分:0)
我建议比较VALUES而不是OBJECTS - 使用==
,而不是===
,基于此:
答案 2 :(得分:0)
这是一个疯狂的猜测,但我怀疑你试图在这里返回值:
res.render('searchResults', {title: 'Weblio', userAdded: users });
如果是这样,添加的用户将拥有 userAdded 下的用户集合,而不是真/假。
当您中断循环并将该变量用作返回值时,您可能需要将布尔值设置为true(例如myVariable = true)。