请看一下,让我知道为什么循环的输出不正确?
基本上我正在遍历用户的friendId数组并通过用户结果进行搜索并查看它们是否匹配,这取决于它应该返回真或假的匹配。
这是我的循环代码:
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 结尾 Jake Jones 以 02 结尾 Jake Macks 以 03 结尾
实际上,Jake Macks 在friendId 中
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