Issue
app.js --------------
const mongoose = require("mongoose");
const utils = require("./config/utils");
const app = express();
utils.initializeDb(mongoose);
app.listen(3000);
config/utils.js
module.exports = (mongoose) => {
initializeDb: async () => {
await mongoose.connect(
process.env.MONGO_URI,
{
useNewUrlParser: true,
},
() => {
console.log("Mongoose connection successfuly started");
}
);
};
};
The error in the terminal/console is: TypeError: utils.initializeDb is not a function.
I'm digging into optimization, encapsulation, to clean more my code passing modules through functions and etc. and I tried this thing but it give me this error... I would like to know the error that is happening in the code and also some tips on how to optimize this code. Thank you :)
Solution
try the below method :
app.js
const mongoose = require("mongoose");
const initializeDb = require("./config/utils");
const app = express();
initializeDb(mongoose);
app.listen(3000);
config/utils.js
module.exports = {
initializeDb: async (mongoose) => {
await mongoose.connect(
process.env.MONGO_URI,
{
useNewUrlParser: true,
},
() => {
console.log("Mongoose connection successfuly started");
}
);
};
};
Answered By - Critical Carpet Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.