Код работает нормально, когда я подключаюсь к mongodb: // localhost: 27017 / myapp
Но когда я пытаюсь подключиться к учетной записи MongoDB в базе данных MLab, код выдает MongoTimeoutError: Тайм-аут выбора сервера через 30000 мс
больше ссылок - https://github.com/Automattic/mongoose/issues/8485
сервер. js
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000;
const mongoose = require(`mongoose`);
const key = require('./config');
const server = http.createServer(app);
server.listen(port, () => {
console.log(`server running on ${port}`);
});
mongoose.set('useUnifiedTopology', true);
mongoose.set('useFindAndModify', false);
const url = process.env.MONGODB_URI || key.MONGO_URL;
const option = {
useNewUrlParser: true
};
mongoose.Promise = global.Promise;
mongoose.connect(url, option).then(() => console.log('Database connected'))
.catch((err) => console.log(err));
ОШИБКА В КОНСОЛИ
at new MongooseTimeoutError (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/node_modules/mongoose/lib/error/timeout.js:22:11)
at NativeConnection.Connection.openUri (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/node_modules/mongoose/lib/connection.js:763:19)
at Mongoose.connect (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/node_modules/mongoose/lib/index.js:332:15)
at Object.<anonymous> (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/server.js:20:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
message: 'Server selection timed out after 30000 ms',
name: 'MongooseTimeoutError',
[Symbol(mongoErrorContextSymbol)]: {} }