循环不会根据查询输出true或false [英] Loop is not outputting true or false based on query
问题描述
请看一下,让我知道为什么循环的输出不正确?
please take a look and let me know why the loop's output is not correct?
基本上,我循环遍历用户的friendId数组,并遍历用户搜索的结果,并查看它们是否匹配,具体取决于匹配结果是否为true或false.
Basically I am looping through the friendId array of a user and through the user results for a search and seeing if they match up, depending on the match it should return true or 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的输出:
Output of 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
The signed in user is John Smith and he searched for Jake
用户: 约翰·史密斯ID以01结尾 杰克·琼斯(Jake Jones)结束于02 杰克·麦克(Jake Macks)结束于03
Users: John Smith id ends in 01 Jake Jones ends in 02 Jake Macks ends in 03
事实上,杰克·麦克斯(Jake Macks)在friendId中
Where in fact Jake Macks is in the friendId
console.log('test');
没有输出,所以我假设它甚至没有进入嵌套循环的if语句
is not being outputed, so I am assuming it is not even going into the if statement of the nested loop
以下是这些控制台日志的输入,我在您移动控制台日志之前就调用它们:
Here is the inputs for these console logs I called right before the console log you moved:
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
推荐答案
您遇到的一个问题是试图记录isFriend结果的位置.在您进入搜索循环时,将isFriend设置为false后,当前正在记录该消息.
One problem you have is the location where you are attempting to log the result of isFriend. It's currently being logged just after you set isFriend to false as you enter the search loop.
该语句需要在内部for循环之后移动.
That statement needs to be moved just after the inner for loop.
在没有所有输入的情况下,很难猜测问题可能出在哪里.
Without having all of the inputs, it makes it very difficult to guess where the problem might be.
使用以下内容作为输入(您能否提供JSON格式的实际输入内容?):
Using the following as input (Can you provide what the actual input is in JSON format?):
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
}
]
除了log语句放置在错误的位置(我不认为我更改了代码的语义)之外,使用这组输入,逻辑仍然有效.您期望的输入可能不是您收到的输入.
Other than the log statement being in the wrong place (I don't think I changed the semantics of your code), with this set of inputs, the logic works. It is likely the input you were expecting is not what you are receiving.
事实证明,OP使用的是nodejs的猫鼬本机驱动程序,经过研究发现,问题的比较部分在这里找到了答案:
It turns out the OP was using the mongoose native driver for nodejs and, after researching found the answer to the comparison portion of the problem here: Comparing mongoose _id and strings
这篇关于循环不会根据查询输出true或false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!