Mongodb: failed to connect to server on first connect
I get the following error:
Warning { MongoError: failed to connect to server [mongodb:27017] on first connect at Pool.
Even though I get this in the terminal window where I run Mongo:
2016-12-25T03:45:23.715+0100 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58868 #8 (8 connections now open)
It kinda looks like there is a connection.
I've tried both
$ mongod
and
$ brew services start mongo
This is my test_helper.js
const mongoose = require('mongoose'); mongoose.connect('mongodb:localhost/users_test'); mongoose.connection .once('open', () => console.log('Good to go!')) .on('error', (error) => {
console.warn('Warning', error);
});
I have not specifically made the db "users_test" as I am under the impression that mongoose or mongo or both will do this on the fly.
I've tried both "localhost" and "127.0.0.1". I'm on OSX 10.11.5 I'm running Node 7.3.0 and Mongo 3.2.9
What am I doing wrong? How do I figure out what's wrong?
By using the below-mentioned codes you can solve mongoerror: failed to connect to server [localhost:27017] on first connect, you can use :
mongoose.connect('mongodb://localhost/users_test');
or
mongoose.connect('localhost/users_test');
or
mongoose.connect('localhost','users_test');
But not mongoose.connect('mongodb:localhost/users_test');it doesn't match the right hostname (mongodb instead of localhost)