2014-11-02 168 views
1
Team.find({ 
     '_id': { $in: [ 
      teamIds 
     ] } 
    }, function(err, teamData) { 
     console.log("teams name " + teamData); 
    }); 

此代碼爲我們提供了未定義回。但在var teamIds是這樣的:

545646d5f5c1cce828982eb7, 
545646d5f5c1cce828982eb8, 
54564af5c9ddf61e2b56ad1e, 
54564c1f1de201782bcdb623, 
54564d2fc660a7e12be6c7a2, 
54564df985495f142c638f9f, 
54564eadb511f1792c9be138, 
54564ec40cf6708a2cd01c81, 
54564ee495f4aea22cf23728 

有誰看到錯誤?

回答

5

如果teamIds已經是一個數組,那麼你不應該把它包裝在另一個數組像你:

Team.find({ 
    '_id': { $in: teamIds } 
}, function(err, teamData) { 
    console.log("teams name " + teamData); 
}); 

或者,如果teamIds僅僅是一個逗號分隔值的字符串,那麼你需要將其轉換爲數組split

Team.find({ 
    '_id': { $in: teamIds.split(',') } 
}, function(err, teamData) { 
    console.log("teams name " + teamData); 
});