UnhandledPromiseRejectionWarning:|弃用警告:|猫鼬5

时间:2018-01-08 08:03:54

标签: javascript node.js mongodb express mongoose

在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问题也无济于事。

我会回避早期版本的猫鼬,但我很想知道解决方案会是什么......

6 个答案:

答案 0 :(得分: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将帮助您删除弃用错误。我希望你能找到这个有用的

答案 1 :(得分:1)

我有一些相同的问题。每当我使用mlab mongodb时,都会出现此问题,但是每当我使用本地数据库时,该问题就会出现。我通过了一些调整猫鼬的选项,如猫鼬官方网站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); 以某种方式为我解决了

答案 2 :(得分:0)

  

试试这段代码

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));

答案 3 :(得分:0)

使用此:

{ useNewUrlParser: true }

代替:

useMongoClient: true

答案 4 :(得分:0)

如何将代码包装在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) } 

答案 5 :(得分:0)

在我没有将我的IP列入mongoDB连接设置白名单之前,我已经处理了10分钟