Trouble issues When I run dev Client - ConnectDB is not a function

Viewed 22

I have an issue, when i run dev client I got an error which is: connectDB is not functions. I tried to fix it with many solutions suggested but i couldn't fix the bug.

There is the structure of my code.

Below the code

    const mongoose = require('mongoose');

const {connectDB} =async () => {
    try {
        const conn = await mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true,useUnifiedTopology: true });

        console.log(`MongoDB connected: ${conn.connection.host}`.cyan.underline)
    } catch (error) {
        console.log(error)
        process.exit(1)
    }
}

module.exports = connectDB

Below the code of server file

 const express = require('express')
//const colors = require('colors')
const dotenv  = require('dotenv')
dotenv.config()
const connectDB = require('./config/db')


const {errorHandler} = require('./middleware/errorMiddleware')
const port = process.env.PORT || 5000

connectDB()

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended:false}))

app.use('/api/tasks', require('./routes/taskRoutes'))
app.use('/api/users', require('./routes/userRoutes'))

app.use(errorHandler)

app.listen(port, () => console.log(`Server started on port ${port}`))

After that i I added this script to package.json : Dev script

Bellow the error message

 C:\Users\hp\Documents\DjamilaWorld\Projects\tasks-app\backend\server.js:11
[0] connectDB()
[0] ^
[0]
[0] TypeError: connectDB is not a function
[0]     at Object.<anonymous> (C:\Users\hp\Documents\DjamilaWorld\Projects\tasks-app\backend\server.js:11:1)
[0]     at Module._compile (node:internal/modules/cjs/loader:1105:14)
[0]     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
[0]     at Module.load (node:internal/modules/cjs/loader:981:32)
[0]     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
[0]     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
[0]     at node:internal/main/run_main_module:17:47
[0] [nodemon] app crashed - waiting for file changes before starting...
[1] (node:16544) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
[1] (Use `node --trace-deprecation ...` to show where the warning was created)
[1] (node:16544) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
[1] Starting the development server...
[1]
[1] Compiled successfully!
[1]
[1] You can now view frontend in the browser.
[1]
[1]   Local:            http://localhost:3000
[1]   On Your Network:  http://192.168.197.1:3000
[1]
[1] Note that the development build is not optimized.
[1] To create a production build, use npm run build.
[1]
[1] webpack compiled successfully
Terminer le programme de commandes (O/N) ? [1] ^C^CTerminer le programme de commandes (O/N)�? ^CTerminer le programme de commandes 
(O/N)�? npm run client exited with code 1
[0] npm run server exited with code 1
0 Answers
Related