Tuesday 23 May 2017

how to resolve - MongoError: failed to connect to server [zzzzz.mlab.com:xxxx] on first connect

I am trying to connect with my mlab database, but i'm getting the following MongoError.

MongoError: failed to connect to server [zzzzz.mlab.com:xxxx] on first connect [MongoError: connect ETIMEDOUT xx.xx.xx.xxx.xxxxx

The following is my api.js file:

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const post = require('../models/post');

const db = "mongodb://uname:pw@dsxxxxx.mlab.com:xxxxx/xxxxx";

mongoose.Promise = global.Promise; //we add this because if we dont, you may get a warning that mongoose's default promise library is deprecated
mongoose.connect(db, function(err) {
    if(err) {
        console.log('Connection error');
    }
});

router.get('/posts', function(req, res) {
    console.log('Requesting posts');
    post.find({})
        .exec(function(err, posts) {
            if (err) {
                console.log('Error getting the posts');
            } else {
                res.json(posts);
                console.log(posts);
            }
        });
});


module.exports = router;

I have installed all the dependencies like mongoose. I'm not quite sure why i am getting this error.

I'd appreciate any guidance on this.

The error is as follows: enter image description here



via Nosail

No comments:

Post a Comment