如何解决 – MongoError:第一次连接时无法连接到服务器

我试图连接我的mlab数据库,但我得到以下的MongoError。

MongoError:无法连接到服务器[zzzzz.mlab.com:xxxx]第一次连接[MongoError:连接ETIMEDOUT xx.xx.xx.xxx.xxxxx

以下是我的api.js文件:

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; 

我已经安装了像mongoose这样的所有依赖。 我不太清楚为什么我得到这个错误。

我会很感激这方面的任何指导。

错误如下: 在这里输入图像说明

你的数据库服务器似乎无法访问,你确定凭证,IP和端口是否正确?