0
私は、nodejsネイティブドライバを使用してmongo db(3.0.6)に複数の方法で接続しようとしています。私はすぐに私のアプリケーションを書くようになったので、私がそこに持っているすべてのものはmongodbとmochaです。MongoClientコールバックが呼び出されなかった
私が試してみても、私はコールバック/約束を実行することができません。
it('init with Promise', function() {
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
console.log("1\n");
//, { server: { auto_reconnect: true } }
MongoClient.connect('mongodb://localhost:27017/session').then(function (db) {
// This line is never called
console.log("2\n");
// Get the collection
var col = db.collection('insert_many_with_promise');
col.insertMany([{a: 1}, {a: 2}]).then(function (r) {
console.log("3\n");
test.equal(2, r.insertedCount);
// Finish up test
db.close();
});
}).catch(function (error) {
console.info(error);
});
})
it('init db', function() {
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/test';
var insertDocument = function (db, callback) {
db.collection('restaurants').insertOne({
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413, 40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [
{
"date": new Date("2014-10-01T00:00:00Z"),
"grade": "A",
"score": 11
},
{
"date": new Date("2014-01-16T00:00:00Z"),
"grade": "B",
"score": 17
}
],
"name": "Vella",
"restaurant_id": "41704620"
}, function (err, result) {
assert.equal(err, null);
console.log("Inserted a document into the restaurants collection.");
callback();
});
};
EDIT: のMongoDBドライバのバージョンここ
は、私が試したいくつかのオプションがある:どちらか私は接続が確立できない場合に発生することができます知っているが、私はすべての例外を得ることはありません: "mongodb": "^ 2.1.4"
おかげで、マウリッツ:行わコールバック(Mochaに非同期コードをテスト参照)をパラメータとして追加!それは魅力のように働いた。しかし、まだまだ私にはたまらないコンサートがあります。私の例は最も基本的でしたが、私の約束事が実際に私が単体テストを書いている別のモジュールの中に入っていればどうですか?完了したコールバックをビジネスロジックに渡すことは、私がやりたい最後のことです。 – yuranos87