1

I'm trying to create a simple rest API with a mongodb.

I have a server.js file

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-Parser');

const app = express();

app.use(bodyParser.json());

const db = require('./config/keys').mongoURI;

mongoose
    .connect('mongodb://test:test123@ds241012.mlab.com:41012/mern_shopping')
    .then(() => console.log('DB Connected'))
    .catch(err => console.log(err));

const port = process.env.PORT || 5000;

app.listen(port, () => console.log('Server started'));

I'm using nodemon

When I run npm run server I get the following error

(node:65209) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

then I got error in console

failed to connect to server

How do I fix this

Update

With {useNewUrlParser: true} the server starts but if I touch the server.js file I get this.

Server started
{ MongoNetworkError: failed to connect to server [ds241012.mlab.com:41012] on first connect [MongoNetworkError: connection 0 to ds241012.mlab.com:41012 timed out]
    at Pool.<anonymous> (/Users/test/Documents/_Work/cd/React/mern-shopping-list/node_modules/mongodb-core/lib/topologies/server.js:564:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/Users/test/Documents/_Work/cd/React/mern-shopping-list/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/Users/test/Documents/_Work/cd/React/mern-shopping-list/node_modules/mongodb-core/lib/connection/connection.js:257:10)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:420:8)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }
RRachel
  • 99
  • 6
  • DeprecationWarning it is not error. It's just a warning. I left your url connection link: newparser. `connect('mongodb://@.mlab.com:41012/mern_shopping',{useNewUrlParser: true})`. – Chance Sep 04 '18 at 13:18
  • Please hide your access credentials.. The connection problem is probably the port of the expression in use. As you can see in the image I used your access data to connect successfully. [IMAGE](https://uploaddeimagens.com.br/imagens/test-jpg-a870cf8c-ac81-498c-8b85-0cb69c9a627f) – Chance Sep 04 '18 at 13:18
  • Try update node version – Mohit Kumar Bordia Sep 04 '18 at 13:19
  • https://stackoverflow.com/questions/51165536/current-url-string-parser-is-deprecated you can check also check @AndersonMendes answer – Anouar Kacem Sep 04 '18 at 13:31
  • I did not understand, in my first cometary I already added the `{useNewUrlParser: true}`. @Anouar Kacem – Chance Sep 04 '18 at 13:40
  • @RRachel, your code works fine in my local system. Can you please post the complete stack trace. – Sridhar Sep 04 '18 at 13:49
  • I have updated my question – RRachel Sep 04 '18 at 15:40

0 Answers0