我正在尝试学习MEAN堆栈,现在我正在使用NodeJS + MongoDB。
在我正在制作的helloworld项目中,我有以下要求:
"dependencies": {
"async": "0.9.0",
"mongodb": "2.0.27"
}
该项目包括在数据库中进行简单插入和查询。在这种情况下,我想通过导演名称查询:
/*
* Finds all documents in the "movies" collection
* whose "director" field equals the given director,
* ordered by the movie's "title" field. See
* http://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html#sort
*/
exports.byDirector = function(db, director, callback) {
db.collection('movies').find({director: director}).toarray(function(error, docs){
callback(error, docs);
});
};
但是,在本教程的测试中,我失败了,收到以下错误:
[14:27:33] Starting 'test'...
[14:27:33] Finished 'test' after 1.1 ms
1) dbInterface can query data by director:
TypeError: db.collection(...).find(...).sort(...).toarray is not a function
at Object.exports.byDirector
Tests failed!
我想我遇到了某种语法错误,但老实说我无法弄清楚它是什么。
我错过了什么?
答案 0 :(得分:1)
我认为你有拼写错误,
不应该对阵阿雷?
exports.byDirector = function(db, director, callback) {
db.collection('movies').find({director: director}).toArray(function(error, docs){
callback(error, docs);
});
};