2013-07-23 90 views
0

循環不輸出真或假請看看並讓我知道爲什麼循環的輸出不正確?根據查詢

基本上我循環瀏覽用戶的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 id結束於01 傑克瓊斯結束於02 傑克麥克斯結束於03

凡其實傑克Macks在friendId

console.log('test'); 

沒有被outputed,所以我假設它甚至沒有進入嵌套循環

這裏的if語句是用於輸入這些控制檯日誌我叫權控制檯登錄之前你感動:

console.log(users); 
console.log(signedInUser); 
console.log(users[x].isFriend); 

的結果是:

[ { firstName: 'Jake', 
    lastName: 'Jones', 
    email: '[email protected]', 
    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: '[email protected]', 
    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: '[email protected]', 
    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: '[email protected]', 
    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: '[email protected]', 
    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: '[email protected]', 
    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 
+1

的輸出是什麼,以及你所期望的輸出? – Dan455

+0

我在搜索結果中添加了針對每個用戶返回爲假2次的輸出... – Lion789

+2

在當前文章中,您的console.log語句在賦值之前。 –

回答

4

您遇到的一個問題是您嘗試記錄isFriend結果的位置。當您在進入搜索循環時將isFriend設置爲false後,它正在記錄。

該語句需要在inner 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,研究後找到了答案,這裏的問題比較部分:Comparing mongoose _id and strings

+0

好的我已經包含了通過console.log調用它們的輸入和結果,因爲你可以看到兩個用戶都在friendId數組中,所以我不確定它爲什麼顯示爲false,甚至沒有進入循環,因爲控制檯.LOG( '試驗');不叫 – Lion789

+0

有你的問題。您正在比較'_id:51ee2017c2023cc816000002'(不是字符串)到''friendId「:」51ee2017c2023cc816000002「'(字符串) – dc5

+0

我在friendId - {friendId:mongoose.Types.ObjectId(req.body。 friendRequest)是不會將其更改爲ID,那麼我該如何解決它? – Lion789

0

這是一種野生的猜測,但我懷疑你正試圖在這裏返回值:

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

如果是這樣,用戶添加會有下的用戶集合userAdded,不是真/假。

當您斷開循環並將該變量用作返回值時,您可能需要將布爾值設置爲true(例如,myVariable = true)。

+0

你是對的,但是,users.isFriend現在應該是一個可以在值no上調用的方法嗎? – Lion789

+0

isFriend值將在集合的每個_element_上,例如:users [0] .isFriend和users [1] .isFriend –

+0

正確,並且它應該返回true或false,取決於循環中的結果是0還是1 ? – Lion789