0

I am beginner level in Node JS and here I am trying to connect to MongoDB.

I am following one tutorial and based on that, I have following project structure:

- server
  - config
    - db.js
  - index.js
- index.js

My mongoose dependency version is: 5.6.9

Inside my db.js, I have following code of lines:

const mongoose = require('mongoose');

// connection to mongoose
export default () => {
    mongoose.connect('mongodb://localhost/dbCollection', function(err, db) {
        if (err) {
            console.log('Unable to connect to the server. Please start the server. Error:', err);
        } else {
            console.log('Connected to Server successfully!');
        }
    });
}

And inside the index.js inside the server folder, I have

import express from 'express';
import dbConfig from './config/db';

const app = express();
dbConfig();

But I am getting error

 _mongoose2.default.connection().once('open', function () {
                       ^

TypeError: _mongoose2.default.connection is not a function
    at exports.default 
4
  • You are using ES6 syntax which is not yet supported by NodeJs by default, so you have 2 options : 1/ In Node9 you can enable Es6 support using node --experimental-modules index.mjs Docs. 2/ Use Es5 syntax : module.exports = () => {...} and const dbConfig = require('./config/db'); Commented Aug 18, 2019 at 9:57
  • @MoadEnnagi which one is better? Commented Aug 18, 2019 at 10:13
  • If you are a beginner I recommend you rewrite it to ES5. Commented Aug 18, 2019 at 11:27
  • @nas ES6 support is Experimental in Node9. Commented Aug 18, 2019 at 11:51

2 Answers 2

1

Try this :

const mongoose = require('mongoose');

const connect = mongoose.connect("mongodb://localhost:27017/dbCollection");

connect.then((db) => {
    console.log('Connected correctly to server');
}, (err) => {
    console.log(err)
    // next(err)
});
Sign up to request clarification or add additional context in comments.

1 Comment

(0, _db2.default)(); ^ TypeError: (0 , _db2.default) is not a function i stil have this err
0

Database Connection And listen Port

// import dependencies
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import logger from 'morgan';// set up dependencies
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false });
app.use(logger('dev'));// set up mongoose

mongoose.connect('mongodb://localhost/projectsupport')
  .then(()=> {
    console.log('Database connected');
  })
  .catch((error)=> {
    console.log('Error connecting to database');
  });

// set up port
    const port = 5035;// set up route
    app.get('/', (req, res) => {
      res.status(200).json({
        message: 'Welcome to Project Support',
      });
    });app.listen(port, () => {
      console.log(`Our server is running on port ${port}`);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.