UnhandledPromiseRejectionWarning:|弃用警告:|猫鼬5
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnhandledPromiseRejectionWarning:|弃用警告:|猫鼬5相关的知识,希望对你有一定的参考价值。
在node.js中构建基本练习聊天应用程序时,我遇到了上述问题。
My express.js code :
var mongoose = require('mongoose')
var dbUrl = 'mongodb://ChatbotAdmin:ChatbotAdmin@ds239177.mlab.com:39177/learning_node'
mongoose.connect(dbUrl, (err) => {
console.log('Connected')
})
This is the full error:
(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问题也没有帮助。
我只想回到早期版本的猫鼬,但我很想知道解决方案是什么......
您应该获得像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将帮助您删除弃用错误。我希望你觉得这有用
我有一些相同的问题。这个问题往往出现,每当我使用mlab mongodb,但每当我使用我的本地db.I传递一些选项调整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);
描述,以某种方式为我解决了它
试试这段代码
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));
用这个:
{ useNewUrlParser: true }
代替:
useMongoClient: true
你怎么把你的代码包装在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) }
在我意识到我没有在mongoDB连接设置上将我的IP列入白名单之前,我正在处理这个问题10分钟
以上是关于UnhandledPromiseRejectionWarning:|弃用警告:|猫鼬5的主要内容,如果未能解决你的问题,请参考以下文章
[Unhandled promise rejection: TypeError: null is not an object (evaluating '_reactNativeImageCropPic