【问题标题】:Mongoose.js how to check if connection successful? [duplicate]Mongoose.js 如何检查连接是否成功? [复制]
【发布时间】:2012-07-12 21:19:51
【问题描述】:

可能重复:
is there a mongoose connect error callback

我使用 mongoose.createConnection 创建连接,它工作在异步模式。

如何检查连接是否成功,如果连接不成功如何报错?

谢谢。

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    is there a mongoose connect error callback 有一个解决方案,你也可以这样做:

    mongoose.connection.on('error', function (err) {
     // Do something
    });
    

    来源:https://github.com/LearnBoost/mongoose/blob/master/lib/connection.js

    【讨论】:

    • var db = mongoose.connection; db.on('error', console.error.bind(console, '连接错误:')); db.once('open', function (callback) { // 耶!}); ......见mongoosejs文档@mongoosejs.com/docs/index.html
    • 这是错误回调,而不是 OP 要求的成功回调。我认为 Maher 的“开放”回调是正确的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多