2011-09-21 17 views
2
IndexedTweets.find(searchParameters, function(err, indexedTweetsResults) { 
    var chunkSize, count, resultArray, size; 
    if (err != null) { 
    return console.log("Error!"); 
    } else { 
    size = indexedTweetsResults.length; 
    count = 0; 
    chunkSize = 100; 
    resultArray = []; 
    indexedTweetsResults.forEach(function(tweet) { 
     console.log(tweet.user); 
    }); 
    } 
}); 

這是我的代碼。我的結果如下:解析一個MongoDB對象(使用Mongoose)時,我怎麼得不到item元素?

{ text: 'stuff', 
    user: 
    { display_name: '...', 
    screen_name: '...'}, 
} 

那麼爲什麼我不能得到tweet.user?它只是返回undefined

+0

它看起來像你顯示的結果實際上是console.log(tweet);除非我錯了。否則,也許tweet.user.user。另外,你在哪裏「得到」它?在相同的forEach或其他地方? – hross

+0

正確 - 但是當我得到'tweet.user'時,它顯示爲未定義。我找到它,然後forEach'd – Shamoon

+0

當你做tweet.user.display_name時會發生什麼?用戶對於某些tweet對象可能是未定義的,無論出於何種原因,但不適用於其他用戶? – hross

回答

0

如果您剛從mongo獲取一串JSON,則需要調用JSON.parse()。如果情況並非如此,那麼你應該提供更多的代碼,因爲它不清楚問題是什麼。

相關問題