猫鼬到mlab的连接问题:UnhandledPromiseRejectionWarning:未处理的承诺被拒绝 [英] mongoose to mlab connection issues : UnhandledPromiseRejectionWarning: Unhandled promise rejection

查看:131
本文介绍了猫鼬到mlab的连接问题:UnhandledPromiseRejectionWarning:未处理的承诺被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const express = require('express');
const app = express();
const mongoose = require('mongoose');
require('./models/users');
require('./services/passport');
const authRoutes = require('./routes/authRoutes');
const Keys = require('./config/dev');

authRoutes(app);

mongoose.connect(Keys.MONGOOSE_URI);

const port = process.env.PORT || 5000;
app.listen(port);

我有一个节点快速应用程序,我尝试使用猫鼬连接到 mlab 数据库.我收到以下异常:

I have a node express application and I trying to connect to mlab db using mongoose. I am getting following exception:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoNetworkError:无法连接到服务器[ds111598.mlab.com:11598] otworkError:连接ECONNREFUSED 35.168.9.109:11598]

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [ds111598.mlab.com:11598] otworkError: connect ECONNREFUSED 35.168.9.109:11598]

我正在使用的技术堆栈:

Tech stack I am using:

"mongoose": "^4.11.1",
node: 6.11.1
npm: 4.6.1

我也尝试使用最新的猫鼬版本,但仍然是同一问题.因此我被困住了.有人可以帮忙吗?

I have tried using the latest mongoose version as well but still the same issue. I am stuck because of this. Can anybody help in this?

谢谢

推荐答案

如果您是公司法人,则必须走出来,否则您可以检查我的代码.

If you are under corporate proxy, you must have to come out else you can check my code.

 const options = {
    useMongoClient: true,
    autoIndex: false, // Don't build indexes
    reconnectTries: 100, // 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
  };
mongoose.connect('mongodb://user:xxxxxx@dsxxxx.mlab.com:667799/DB_NAME',options).then(
  ()=>{
    console.log("connected to mongoDB")},
 (err)=>{
     console.log("err",err);
}

)

这篇关于猫鼬到mlab的连接问题:UnhandledPromiseRejectionWarning:未处理的承诺被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆