Search code examples
reactjsnode.js

react js type error when connecting to db


I am getting a type error when I am trying to load my db connection. My db.js file:

const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost:27017/mydatabase';

let db = null;

async function connectDB() {
console.log("Did you make it");
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology:true});
try {
    await client.connect();
    db = client.db();
    console.log("connected to MongoDB");
} catch(err) {
    console.error("Could not connect to MongoDB", err);
}
}

My server.js file

// import DB connection
const { connectDB } = require('./db.js');
const app = express();
const port = process.env.port || 3000;
app.get('/', (req,res) => {
res.send('Hello from Express and MongoDB');
});

connectDB().then(() => {
   app.listen(port, () => {
    console.log('Server is running on port ',port);
   });
});

If I comment out connectDB().then(() => { and the closing });. The page loads. So I know that part is correct. The error I get when I leave it in. server.js:13

connectDB().then(() => {
TypeError: connectDB is not a function.

It appears to be a function to me, but I don't know react so any help would be really appreciated.


Solution

  • Hello I have read carefully and see the problem

    You are using require to import connectDB, but in the export statement of db.js, you're not explicitly exporting it as a named export. Instead, you're defining the connectDB function inside the db.js file but not exporting it properly.

    Solution

    To resolve this, you need to properly export connectDB from the db.js file and make sure you're importing it correctly in your server.js file.

    Export connectDB from db.js

    In your db.js, you should explicitly export the connectDB function like this:

    // db.js
    const { MongoClient } = require('mongodb');
    const uri = 'mongodb://localhost:27017/mydatabase';
    
    let db = null;
    
    async function connectDB() {
        console.log("Did you make it");
        const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology:true });
        try {
            await client.connect();
            db = client.db();
            console.log("connected to MongoDB");
        } catch (err) {
            console.error("Could not connect to MongoDB", err);
        }
    }
    
    // Export the connectDB function
    module.exports = { connectDB };
    

    Import connectDB in server.js

    Now, in your server.js file, make sure you're correctly importing the connectDB function using require():

    // server.js
    const express = require('express');
    // Import the connectDB function correctly
    const { connectDB } = require('./db.js');
    const app = express();
    const port = process.env.port || 3000;
    
    app.get('/', (req, res) => {
        res.send('Hello from Express and MongoDB');
    });
    
    connectDB().then(() => {
        app.listen(port, () => {
            console.log('Server is running on port ', port);
        });
    });