UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoError:首次连接时无法连接到服务器 [英] UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server on first connect

查看:5905
本文介绍了UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoError:首次连接时无法连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MEAN堆栈开发的应用程序.我收到以下错误

I have an app developed using MEAN stack. I am getting the following error

(节点:4920)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoError:首次连接[Mon时无法连接到服务器[ds047865.mongolab.com:47865] goError:连接ECONNREFUSED 130.211.211.211:47865]

(node:4920) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [ds047865.mongolab.com:47865] on first connect [Mon goError: connect ECONNREFUSED 130.211.211.211:47865]

(节点:4920)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝.将来,未处理的承诺拒绝将终止Node.js进程,并以 非零退出代码

(node:4920) [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

这是我的代码:

    var mongoose = require('mongoose');
var validator = require('mongoose-unique-validator'); 
var nodemailer = require('nodemailer');
mongoose.connect('mongodb://naresh:naresh@ds249025.mlab.com:49025/languageapp', { useMongoClient: true });  
var db = mongoose.connection;
db.on('connect', function () { console.log('connected'); });


var userSchema = new mongoose.Schema({
    userName: { type: String, required: true, index: true, unique: true },
    userEmail: { type: String, required: true, index: true, unique: true },
    userPass: { type: String, required: true },
    Date: { type: String, default: Date.now() },
    userResult: String
});
var questionSchema = new mongoose.Schema({
    question: { type: String, unique: true },
    op1: String,
    op2: String,
    op3: String,
    op4: String,
    rightAnswer: String,
    questionType: String,
    questionID: String,
    quizTopic: String,
    CreatedAt: { type: String, default: Date.now() }
});
var resultSchema = new mongoose.Schema({
    userID: String,
    userName: String,
    userMatriculation: String,
    userEmail: String,
    quizTopic: String,
    userResult: String,
    date: { type: String, default: Date.now() }
});
userSchema.plugin(validator);
var User = mongoose.model('Users', userSchema);
var Question = mongoose.model('Questions', questionSchema);
var Result = mongoose.model('Results', resultSchema);

尽管出现这些错误,但我没有使用任何诺言.

I am not using any promises though am getting these errors.

推荐答案

根据您的连接字符串'mongodb://naresh:naresh@ds249025.mlab.com:49025/languageapp',dbuser是naresh,而dbpassword也是naresh.因此,您必须确保已经(显式)创建了具有相同凭据的数据库用户(dbuser).这与您登录mlab.com所用的凭据不同.

According to your connection string 'mongodb://naresh:naresh@ds249025.mlab.com:49025/languageapp', the dbuser is naresh and the dbpassword is also naresh. So you have to make sure that you have already created (explicitly) a database user (dbuser) with that same credential. This is NOT the same credential that you use to logon to mlab.com.

要创建数据库用户,请访问mlab.com门户中的用户"实验室,然后单击添加数据库用户".

To create the database user, go to the Users lab in the mlab.com portal and click on "Add database user".

这篇关于UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoError:首次连接时无法连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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