2016-04-24 8 views
1

私はMongoDbについて学習しています。モジュールの演習は、渡されたが通過しないパラメータ "Director"でデータベース内の一致を返すMongoDbの関数ですテスト。これは私のコードです:movies.js関数のエラーがテストに合格しない

 exports.byDirector = function(db, director, callback) { 
    // TODO: implement 

    db.collection('movies').find(
    {"director" : director}).sort({title : 1}).toArray(function(error, docs){ 
    if (error){ 
     console.log(error); 
     process.exit(1); 
    } 
    docs.forEach(function(doc){ 
     console.log(JSON.stringify(doc)); 
    }); 
    process.exit(0); 
    }) 
    callback(null, []); 
}; 

これは、関数のテストです:

it('returns multiple results ordered by title', function(done) { 
    dbInterface.byDirector(db, 'George Lucas', function(error, docs) { 
     assert.ifError(error); 
     assert.ok(Array.isArray(docs)); 
     assert.equal(docs.length, 4); 
     assert.equal(docs[0].title, 'Attack of the Clones'); 
     assert.equal(docs[1].title, 'Revenge of the Sith'); 
     assert.equal(docs[2].title, 'Star Wars'); 
     assert.equal(docs[3].title, 'The Phantom Menace'); 
     docs.forEach(function(doc) { 
     delete doc._id; 
     }); 
     assert.deepEqual(Object.keys(docs[0]), ['title', 'year', 'director']); 
     ++succeeded; 
     georgeLucasMovies = docs; 
     done(); 
    }); 
    }); 
    var succeeded = 3; 

間違っているのですか? ありがとうございました。

答えて

1

あなたはこの

exports.byDirector = function(db, director, callback) { 
    // TODO: implement 

    db.collection('movies').find(
    {"director" : director}).sort({title : 1}).toArray(function(error, docs){ 
    if (error){ 
     callback(err, null); 
    } 
    docs.forEach(function(doc){ 
     console.log(JSON.stringify(doc)); 
    }); 
    callback(null, docs); 
    }); 
}; 
+1

はどうもありがとうございます試してみてくださいtoArrayコールバック関数

callback関数を呼び出しています。それは完全に動作します – TibyDublin

関連する問題