colloqi/pisignage-server

MongoDB URI String not working

Blake254 opened this issue · 4 comments

I am trying to change the URI in development.js from:

`
'use strict';

module.exports = {
env: 'development',
https: false,
port: process.env.PORT || 3000,
mongo: {
uri: 'mongodb://localhost/pisignage-dev',
options: {useMongoClient: true}
}
};
`

To use:

`
'use strict';

module.exports = {
env: 'development',
https: false,
port: process.env.PORT || 3000,
mongo: {
uri: 'mongodb://[username]:[password]@cluster0.a0oba.mongodb.net/db?retryWrites=true&w=majority'
,
options: {useMongoClient: true}
}
};
`

And it keeps telling me that the mongo db process is not running and stops the server, any ideas?

Mongodb service either not installed or not enabled in your server OS.

Please provide below info
1- Server OS
2- mongo version and how it was installed.
3- error when you run mongod manually.

And hope you have access to your mongo server given in URI.

in the file server.js (pisignage-server directory) at line 21, add console.log to see the error as shown, also you can avoid program exit if needed.

mongoose.connect(config.mongo.uri, config.mongo.options,function(error){
if (error) {
console.log(error); //ADD this line
console.log('');
console.log('* MongoDB Process not running *');
console.log('
\n');

     process.exit(1);    //COMMENT this line if you do not want to exit
}

});

The error that is logged is below. I know that the connection string works that is used is not incorrect as I can successfully connect via MongoDB Compass.

Error [MongoError]: failed to connect to server [cluster0.a0oba.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND cluster0.a0oba.mongodb.net
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) {
  name: 'MongoError'
}]
    at Pool.<anonymous> (C:\Users\USER\Desktop\Pi\pidev2\pisignage-server\node_modules\mongodb-core\lib\topologies\server.js:336:35)
    at Pool.emit (events.js:315:20)
    at Connection.<anonymous> (C:\Users\USER\Desktop\Pi\pidev2\pisignage-server\node_modules\mongodb-core\lib\connection\pool.js:280:12)
    at Object.onceWrapper (events.js:422:26)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (C:\Users\USER\Desktop\Pi\pidev2\pisignage-server\node_modules\mongodb-core\lib\connection\connection.js:189:49)
    at Object.onceWrapper (events.js:422:26)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:12236) UnhandledPromiseRejectionWarning: MongoError: failed to connect to server [cluster0.a0oba.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND cluster0.a0oba.mongodb.net
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) {
  name: 'MongoError'
}]
    at Pool.<anonymous> (C:\Users\USER\Desktop\Pi\pidev2\pisignage-server\node_modules\mongodb-core\lib\topologies\server.js:336:35)
    at Pool.emit (events.js:315:20)
    at Connection.<anonymous> (C:\Users\USER\Desktop\Pi\pidev2\pisignage-server\node_modules\mongodb-core\lib\connection\pool.js:280:12)
    at Object.onceWrapper (events.js:422:26)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (C:\Users\USER\Desktop\Pi\pidev2\pisignage-server\node_modules\mongodb-core\lib\connection\connection.js:189:49)
    at Object.onceWrapper (events.js:422:26)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12236) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Issue solved. URI needed to be:

uri: 'mongodb+srv://[username]:[password]@cluster0.a0oba.mongodb.net/db?retryWrites=true&w=majority'

Was not working because MongoDB was out of date. If you want to use the mongodb+srv:// version then you need a driver of version 3.0.0 or higher.