skip to Main Content

TypeError: connectToMongo is not a function

My code (Index.js)

const connectToMongo = require('./db')
connectToMongo();

My code (db.js)

const mongoose = require('mongoose');
const mongoURI = "mongodb://localhost:27017"

const connectToMongo = ()=> {
    mongoose.connect(mongoURI,()=>{
        console.log("Connected to mongo successfully")
    })
}

module.export = connectToMongo;

Anyone can help?

2

Answers


  1. I think this line

    module.export = connectToMongo;
    

    should be

    module.exports = connectToMongo;
    
    Login or Signup to reply.
  2. You need to change the line of

    module.export = connectToMongo;
    

    to

    module.exports = connectToMongo;
    

    See: https://www.freecodecamp.org/news/module-exports-how-to-export-in-node-js-and-javascript/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search