2

我正在尝试创建一个 grunt 任务以通过 mongoose 删除 mongodb 数据库。连接挂起,我必须强制退出,它实际上并没有删除数据库或输出错误。

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
4

1 回答 1

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);
  });
});
});
于 2013-05-01T00:22:13.310 回答