MongoError: Failed to connect to server on first connection - offline only

I get this error when trying to connect to MongoClient.connect, but only when I'm online. As soon as I am online, without changing the code or even restarting mongoDB, my application connects every time.

AssertionError: null == {MongoError: failed to connect to server [localhost: 27017] on the first connection

My server.js looks like this:

// server.js

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var operations = require('./operations');
// Connection URL
var url = 'mongodb://localhost:27017/myApp';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  assert.equal(null, err);
...
Run code

If I just use > mongo in the terminal, it connects without problems even in offline mode. Am I missing something in the documentation?

+4
source share
1 answer

! , Mongoose , - , Mongoose, .

+1

Source: https://habr.com/ru/post/1652508/


All Articles