2017-08-27 75 views
0

感謝大家一直支持讓這個難題的第一步特別是@GaëtanRouziès和@Neil解決。在此thread上,代碼完美適用於有問題的場景。我想調整相同的代碼來處理另一個類似的場景,但我還沒有弄清楚可能會出現什麼問題。這一次,我有一個coursemodule集合,這是課程和模塊之間的許多關係集合,它們只存儲coursesIdmoduleId。由於代碼非常完美,我簡單地複製,做了一點修改,並得出了下面的代碼:做1:很多:許多人加入mongodb

courses(){ 
    var theslug = FlowRouter.getParam('myslug'); 
    var mySchoolDocs = SchoolDb.findOne({slug: theslug}); 
    var arrayModuleSchools = ModuleSchool.find({schoolId: mySchoolDocs._id}); 
    // Transform the array of document into an array with only the ids 
    var arrayModuleId = []; 
    arrayModuleSchools.forEach(function(moduleSchools){ 
     arrayModuleId.push(moduleSchools.moduleId); 
    }); 
    var coursetoMod = CourseModules.find({}, {moduleId: {$in: arrayModuleId}}); 
    if (coursetoMod) { 
     coursesArrayIds = []; 
     console.log(coursetoSchool); 
     coursetoMod.forEach(function (courseToModules) { 
      coursesArrayIds.push(courseToModules.coursesId); 
     }); 
     return Courses.find({_id: {$in: coursesArrayIds}}).fetch(); 
    } 
} 

具體而言,在Modules集合中只存在2個模塊,與IDS - xfLM9DEzhCMYQpQ32 and PTbZQ9cTG9pByFsY2。該CourseModule收藏有這有文檔:

{ 
    "_id" : "iXX4unJZRNcCw9bAm", 
    "moduleId" : "PTbZQ9cTG9pByFsY2", 
    "coursesId" : "FbgcdZxADHKRBj98z", 
    "createdAt" : ISODate("2017-08-25T16:36:17.173Z"), 
    "userId" : "n5rqFSHbhm7zqADyB" 
} 
{ 
    "_id" : "RAJJFjqAjGoDeNhko", 
    "moduleId" : "PTbZQ9cTG9pByFsY2", 
    "coursesId" : "ESAf6NGpZzXeioecp", 
    "createdAt" : ISODate("2017-08-25T16:36:17.182Z"), 
    "userId" : "n5rqFSHbhm7zqADyB" 
} 
{ 
    "_id" : "8ceuFwZK8Qduo5J5P", 
    "moduleId" : "xfLM9DEzhCMYQpQ32", 
    "coursesId" : "KnNj4GLcyMtvF8JmB", 
    "createdAt" : ISODate("2017-08-25T16:38:15.368Z"), 
    "userId" : "n5rqFSHbhm7zqADyB" 
} 

點在哪裏我登錄到我的selectorId未定義控制檯:

1 ... n.Cursor {集合:LocalCollection,分揀機:空,匹配: 中號... o.Matcher,_selectorId:未定義,跳過:未定義...} _projectionFn: (OBJ)_selectorId:undefined_transform:nullcollection: LocalCollectionfields:undefinedlimit:undefinedmatcher: Minimongo.Matcherreactive:trueskip:undefinedsorter:null__proto__: Object_depend:(changers,_allow_unordered)_getCollectionName: ()_getRawObjects :(選項)_publishCursor:(子)構造函數: count :()fetch :()forEach :(callback, thisArg)getTransform :()map:(callback,thisArg)觀察: (options)observeChanges:(options)rewind :()proto:Object view.js:30 L ... n.Cursor {collection:LocalCollection,sorter:null, Matcher:M ... o.Matcher,_selectorId:undefined,skip:undefined ...}

我想要做的就是獲取當前通過模塊顯示的特定學校附加的課程。

+0

哪個控制檯產生的錯誤信息?鉻調試器控制檯?或者「流星蒙哥」控制檯?或者'流星'服務器控制檯? –

回答

1

您正在使用find功能走錯了路:

var coursetoMod = CourseModules.find({}, {moduleId: {$in: arrayModuleId}}); 

find()函數有兩個參數:myCollection.find(query, projection)。當按字段過濾文檔時,它必須在query參數內。而projection參數用於選擇要返回的字段。

對於您的情況,以下是您正在使用的參數:query: {}projection: {moduleId: {$in: arrayModuleId}}。但它需要:query: {moduleId: {$in: arrayModuleId}}

所以,你只需要使用$in作爲第一個參數

var coursetoMod = CourseModules.find({moduleId: {$in: arrayModuleId}}); 

順便說一句,如果你想直接看到裏面的find函數返回的文件一個console.log,使用.fetch()

var coursetoMod = CourseModules.find({moduleId: {$in: arrayModuleId}}).fetch(); 

MongoDB中找到函數文檔:https://docs.mongodb.com/manual/reference/method/db.collection.find/

+0

仍然得到控制檯上的同樣的信息:'''1 ... n.Cursor {集合:LocalCollection,分揀機:空,匹配:M ... o.Matcher,_selectorId:未定義,跳過:未定義...}''' – kehinde

+0

當我添加.fetch到查詢它返回一個空數組。 '''[]''' – kehinde

+1

第一條消息是正常的,導致find()返回一個Cursor對象。使用fetch(),我們知道它返回0文檔。你需要檢查更多的數據:'console.log(coursetoMod);的console.log(arrayModuleId);的console.log(CourseModules.find({})取()); console.log(coursesArrayIds);'在調試時,如果您不確定,請檢查更多數據。 –

1

@gaetan是在正確的軌道的答案,你需要使用的查詢參數,而不是投影參數。在代碼中還可以使用與Meteor一起打包的underscore庫進行一些其他簡化。

courses() { 
    const slug = FlowRouter.getParam('myslug'); 
    const schoolId = SchoolDb.findOne({ slug })._id; 
    const Modules = ModuleSchool.find({ schoolId }); 
    const ModuleIds = _.pluck(Modules,'moduleId'); 
    const coursetoMod = CourseModules.find({ moduleId: { $in: ModuleIds }}); 

    if (coursetoMod.length) { 
    coursesIds = _.pluck(coursetoMod,'coursesId'); 
    return Courses.find({ _id: { $in: coursesArrayIds }}).fetch(); 
    } 
}