Sambulo Senda
Sambulo Senda

Reputation: 1418

Mongo DB connection

I dont know why my code is not compiling, I have the following code.

mongoose.connect(db)
         .then( onFulfilled: () => console.log(`MongoDB connected.`))
         .catch( onRejected: err => console.log(err));

This is the error I get

[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
/home/sambulo/Documents/linkshortenner/server.js:8
         .then( onFulfilled: () => console.log(`MongoDB connected.`))
                ^^^^^^^^^^^

SyntaxError: missing ) after argument list
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

Upvotes: 0

Views: 111

Answers (3)

Neel Rathod
Neel Rathod

Reputation: 2111

A Promise has a two methods then() and catch(),

Promise.then(() => {});
Promise.catch(() => {});
Promise
    .then(() => {})
    .catch(() => {});

In your code you need to remove : and update your code little bit...

mongoose.connect(db)
         .then((onFulfilled) => console.log(`MongoDB connected.`))
         .catch((err) => console.log(err));

Upvotes: 1

Craig van Tonder
Craig van Tonder

Reputation: 7677

.then( onFulfilled: () => console.log(`MongoDB connected.`))

Not entirely sure what you are trying to do here, it seems like you're declaring an object within the then clause where you actually want to have a function:

.then( () => console.log(`MongoDB connected.`) )

or:

.then( function onFulfilled () { console.log(`MongoDB connected.`) } )

EDIT: Same applies for catch()

Upvotes: 1

Shams Nahid
Shams Nahid

Reputation: 6559

Just remove the onFulfilled and onRejected

Then make sure the db contains the database address.

Your final code should look like this,

mongoose.connect(db)
         .then(() => console.log(`MongoDB connected.`))
         .catch((err) => console.log(err));

Upvotes: 1

Related Questions