UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoError:第一次连接时无法连接到服务器

时间:2017-11-24 20:28:55

标签: node.js mongodb express mean-stack

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

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

(node:4920)[DEP0018]弃用警告:不推荐使用未处理的拒绝承诺。将来,未处理的承诺拒绝将使用a终止Node.js进程 非零退出代码

这是我的代码:

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

虽然我遇到了这些错误,但我没有使用任何承诺。

1 个答案:

答案 0 :(得分:0)

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

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