0
votes

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

(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]

(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

here is my code:

    var mongoose = require('mongoose');
var validator = require('mongoose-unique-validator'); 
var nodemailer = require('nodemailer');
mongoose.connect('mongodb://naresh:[email protected]: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.

1
Something somewhere is trying to make a database connection via a promise call, the call is failing and the reject of the promise is unhandled. If you don't post any code we can't help you any more than that. - Kevin
Please check the code. I am not using promises anywhere in the code though getting this error. - Naresh Aithagoni
Try connecting manually to the database with a mongo shell. You can also add a callback to the connect method to handle the error message. See here. Finally, I think the connected event is called connected, not connect. - Mika Sundland
I am not getting these errors when I run the app at my home but I am getting these errors only when I run the app at my university. Is it the problem of the network or something like this? - Naresh Aithagoni
It's most likely a connection issue. A firewall or something similar could be the problem. ECONNREFUSED is the important part. You can Google it and try to look at other people's solution to the problem. I doubt it can be solved by code. UnhandledPromiseRejectionWarning is not the essential part. It just means you are not handling the promise that connect returns. It will most likely go away if you chain a catch() to it. - Mika Sundland

1 Answers

0
votes

According to your connection string 'mongodb://naresh:[email protected]: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.

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