0

请看一下,让我知道为什么循环的输出不正确?

基本上我正在遍历用户的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
4

3 回答 3

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
  }
]

除了 log 语句位于错误的位置(我认为我没有更改代码的语义)之外,使用这组输入,逻辑是有效的。您期望的输入很可能不是您收到的。

事实证明,OP 使用的是用于 nodejs 的 mongoose 本机驱动程序,并且在研究后找到了问题比较部分的答案:Comparing mongoose _id and strings

于 2013-07-23T17:17:03.440 回答
0

我建议比较 VALUES,而不是 OBJECTS - use ==, not ===,基于此:

为什么 Array.indexOf 找不到外观相同的对象

于 2013-07-23T16:05:44.743 回答
0

这是一个疯狂的猜测,但我怀疑您正试图在此处返回值:

res.render('searchResults', {title: 'Weblio', userAdded: users });

如果是这样,添加的用户将在userAdded下拥有用户集合,而不是真/假。

当您中断循环并将该变量用作返回值时,您可能需要将布尔值设置为 true(例如 myVariable = true)。

于 2013-07-23T16:09:01.100 回答