【问题标题】:UnhandledPromiseRejectionWarning: | DeprecationWarning: | Mongoose 5未处理的承诺拒绝警告:|弃用警告:|猫鼬 5
【发布时间】:2018-01-08 08:03:54
【问题描述】:

在 node.js 中构建基本练习聊天应用时,我遇到了上述问题。

我的 express.js 代码:

var mongoose = require('mongoose')
var dbUrl = 'mongodb://ChatbotAdmin:ChatbotAdmin@ds239177.mlab.com:39177/learning_node'
mongoose.connect(dbUrl,  (err) => {
console.log('Connected')
})

这是完整的错误:

(node:10192) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): MongoNetworkError: connection 0 to 
ds239177.mlab.com:39177 closed
(node:10192) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

我尝试添加 {useMongoClient: true},就像这个家伙 The options [useMongoClient] is not supported 一样。只是发现他在猫鼬 5 中所做的那样,没有必要(也没有帮助)。

我进一步研究了:

mongoose.Promise = global.Promise

我有同样的错误。

This 问题也没有帮助。

我只是回退到早期版本的 mongoose,但我很想知道解决方案是什么......

【问题讨论】:

  • 为了清楚起见,其余代码中没有使用任何承诺。
  • 当你从猫鼬那里得到任何结果时处理你的承诺
  • @ManjeetThakur 不清楚。另外,正如我刚刚提到的,我还没有使用任何承诺。
  • 真正的错误是MongoNetworkError: connection 0 to ds239177.mlab.com:39177 closed,来自mongodb-core。
  • @MikaS 究竟是什么意思?

标签: javascript node.js mongodb express mongoose


【解决方案1】:

您应该获得像 bluebird 这样的第三方承诺库。 见下文:

mongoose.Promise = require('bluebird');
DBURL = process.env.DBURL;

var options = {
useMongoClient: true,
  socketTimeoutMS: 0,
  keepAlive: true,
  reconnectTries: 30
};

mongoose.connect(DBURL, options);
db = mongoose.connection;
db.on('error', err => {
  console.log('There was a db connection error');
});
db.once('connected', () => {
  console.log('Successfully connected to ' + DBURL);
});
db.once('disconnected', () => {
  console.log('Successfully disconnected from ' + DBURL);
});
process.on('SIGINT', () => {
  mongoose.connection.close(() => {
    console.log('dBase connection closed due to app termination');
    process.exit(0);
  });
});

Bluebird 将帮助您消除弃用错误。希望对您有所帮助

【讨论】:

    【解决方案2】:

    我有一些同样的问题。每当我使用 mlab mongodb 时都会出现这个问题,但每当我使用本地 db 时都可以。我通过了一些调整 mongoose 的选项,如 mongoose 官方网站 https://mongoosejs.com/docs/connections.html 所述 const options = { useNewUrlParser: true, autoIndex: false, // Don't build indexes reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect reconnectInterval: 500, // Reconnect every 500ms poolSize: 10, // Maintain up to 10 socket connections // If not connected, return errors immediately rather than waiting for reconnect bufferMaxEntries: 0, connectTimeoutMS: 10000, // Give up initial connection after 10 seconds socketTimeoutMS: 45000, family: 4 // Use IPv4, skip trying IPv6 }; mongoose.connect(my_mongo_url,options); 不知何故为我解决了它

    【讨论】:

    【解决方案3】:

    试试这个代码

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
      console.log("h");
    });
    
    //----------------------------
     Second option
    
    // connect to  mongoose
    mongoose.connect('mongodb://localhost/peppino-calc', {
      useMongoClient: true
    })
    .then(() => { console.log('MongoDB connected...')})
    .catch(err => console.log(err));
    

    【讨论】:

    • 我已经编辑了我的答案,请检查这是否适合你
    • 你能告诉我你的问题吗
    【解决方案4】:

    使用这个:

    { useNewUrlParser: true }
    

    代替:

    useMongoClient: true
    

    【讨论】:

      【解决方案5】:

      如何将代码包装在 try catch 中?

       var mongoose = require('mongoose')
       var dbUrl = 'mongodb://ChatbotAdmin:ChatbotAdmin@ds239177.mlab.com:39177/learning_node'
      
       try {
         mongoose.connect(dbUrl, { useMongoClient: true })
       } catch(e) { console.log(e.message) } 
      

      【讨论】:

        猜你喜欢
        • 2020-01-01
        • 2016-11-03
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        相关资源
        最近更新 更多