我想通过nodejs脚本填充一些mongodb实例和一些虚假数据,我想在for循环完成时调用mongoose.disconnect(),这样程序就不会因为mongoose实例而继续循环。
问题是,因为nodejs在当前代码中是非阻塞的,所以它调用for循环然后继续并退出。 我已经尝试过async lib,但无法弄清楚我是如何做到这一点的。
在节点中执行此操作的正确方法是什么?
'use strict';
var mongoose = require('mongoose'),
promise = require('promise');
mongoose.Promise = promise;
var uri = 'mongodb://localhost:27017/test'
var test1Schema = new mongoose.Schema({
name: String,
type: String
});
var test2Schema = new mongoose.Schema({
name: String,
type: String
});
var test3Schema = new mongoose.Schema({
name: String,
type: String
});
var test1 = mongoose.model('test1', test1Schema);
var test2 = mongoose.model('test2', test2Schema);
var test3 = mongoose.model('test3', test3Schema);
function genRandom (high, low) {
return Math.floor(Math.random() * (high - low) + low);
}
mongoose.connect(uri);
var _connection = mongoose.connection;
_connection.on('error', function (err) {
console.log('Couldn\'t connect to the database %s - %s', uri, err);
});
_connection.on('open', function () {
console.log('Connection open - ', uri);
});
_connection.on('close', function () {
console.log('Closed mongoose connection');
});
var saveCallback = function (err) {
if (err) {
console.log('An error occured while saving %s', err);
}
}
function genRecords (callback) {
for (var i=0; i <= 5; i++) {
new test1({
name: 'test_' + i.toString(),
type: genRandom(123, 321).toString()
}).save(callback);
new test2({
name: 'test_' + i.toString(),
type: genRandom(456, 654).toString()
}).save(callback);
new test3({
name: 'test_' + i.toString(),
type: genRandom(789, 987).toString()
}).save(callback);
}
}
genRecords(saveCallback)
mongoose.disconnect();
输出:
Connection open - mongodb://localhost:27017/test
Closed mongoose connection
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
An error occured while saving MongoError: server localhost:27017 sockets closed
答案 0 :(得分:0)
您必须认为使用异步函数。因此,您正在准备save()
个功能,但在断开连接后,他们已完成。
您需要等待save()
来电结束。两种解决方案:
答案 1 :(得分:-1)
使用此模块http://caolan.github.io/async/
然后执行此代码。
async.times(5, function(i, done){
//run in here your 3 instances with async.waterfall or something like this then go to next iteration
methodToSave3Instances(i, done);
}, function(err) {
mongoose.disconnect();
});
function methodToSave3Instances(i , cb) {
async.waterfall([
function(callback) {
new test1({
name: 'test_' + i.toString(),
type: genRandom(123, 321).toString()
}).save(callback);
},
function(callback) {
new test2({
name: 'test_' + i.toString(),
type: genRandom(123, 321).toString()
}).save(callback);
},
function(callback) {
new test3({
name: 'test_' + i.toString(),
type: genRandom(123, 321).toString()
}).save(callback);
}
], function (err, result) {
cb();
});
}