我正在尝试创建一个通过mongoose删除mongodb数据库的grunt任务。连接挂起,我必须强行退出,它实际上并没有丢弃数据库或输出错误。
var db = require('./db/schema');
grunt.registerTask('drop', 'drop the database', function() {
// async mode
var done = this.async();
db.mongoose.connection.db.dropDatabase(function(err) {
if(err) {
console.log(err);
} else {
console.log('Successfully dropped db');
}
db.mongoose.connection.close(done);
});
});
输出
$ grunt drop
Running "drop" task
Successfully connected to: mongodb://localhost/test
^C
答案 0 :(得分:2)
最后在这里找到了我的问题的解决方案:https://groups.google.com/forum/?fromgroups=#!topic/mongoose-orm/Cck_VND80r8必须将所有内容包装在connection.on
grunt.registerTask('drop', 'drop the database', function() {
// async mode
var done = this.async();
db.mongoose.connection.on('open', function () {
db.mongoose.connection.db.dropDatabase(function(err) {
if(err) {
console.log(err);
} else {
console.log('Successfully dropped db');
}
db.mongoose.connection.close(done);
});
});
});