Javascript 猫鼬赢了';t从预先存在的数据库node.js检索数据
我已经尽了我所能,我在谷歌上搜索并找到了一些例子,我尝试了这些例子,但没有任何乐趣。我现在真的被卡住了。因此,我在我的Mac上安装了mongodb,它是通过brew安装的。进展顺利。我用“mongod”启动服务器,它也运行得很好。我在mongo interactive上插入了一些数据,当我检索数据时,您可以在下面看到这些数据。我有数据库名“test”和集合名“test” 现在,当我用这段代码用mongoose创建一个简单的mocha测试时Javascript 猫鼬赢了';t从预先存在的数据库node.js检索数据,javascript,node.js,mongodb,mongoose,mocha.js,Javascript,Node.js,Mongodb,Mongoose,Mocha.js,我已经尽了我所能,我在谷歌上搜索并找到了一些例子,我尝试了这些例子,但没有任何乐趣。我现在真的被卡住了。因此,我在我的Mac上安装了mongodb,它是通过brew安装的。进展顺利。我用“mongod”启动服务器,它也运行得很好。我在mongo interactive上插入了一些数据,当我检索数据时,您可以在下面看到这些数据。我有数据库名“test”和集合名“test” 现在,当我用这段代码用mongoose创建一个简单的mocha测试时 var Vocabulary = function()
var Vocabulary = function() {
function get(german_vocab) {
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/test');
mongoose.connection.on("open", function(){
console.log("mongodb is connected!!");
});
mongoose.connection.db.collection("test", function (err, collection) {
collection.find().toArray(function(err, results) {
console.log(results);
});
});
}
return {
get : get
};
}
module.exports = Vocabulary;
这是我的摩卡咖啡测试
var should = require('should');
var Vocabulary = require('../modules/vocabulary');
describe("Vocabulary", function() {
it("should get a translation of Ich", function() {
var vocabulary = Vocabulary();
vocabulary.get("Ich");
});
});
这是我从摩卡得到的
Vocabulary
✓ should get a translation of Ich (161ms)
✔ 1 test complete (163ms)
如您所见,它从不打印“mongodb已连接!”,在find()方法上也不打印任何内容
请帮帮我。非常感谢。我认为基本问题是您试图采用同步方法来处理异步活动。例如:
get
方法应该在传入函数的回调中返回结果done
函数参数,该参数在测试完成时传递到it
回调中我认为最基本的问题是,您试图对异步活动采用同步方法。例如:
get
方法应该在传入函数的回调中返回结果done
函数参数,该参数在测试完成时传递到it
回调中
Vocabulary
✓ should get a translation of Ich (161ms)
✔ 1 test complete (163ms)