Express router is not working on mongoose. It is saying User is not defined


**App.js Code**

const express = require("express");
const { default: mongoose } = require("mongoose");
const gaming = require("./routes/gaming");
const app = express();

app.use("/", gaming);

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

const mySchema = new mongoose.Schema({
    name: String,
    genre: String,
    games: String,

const User = mongoose.model('gaming', mySchema);

app.listen(8000, ()=>{
    console.log("listing at port 8000");

***Routes Folder Code***

const express = require("express");
let router = express.Router();"/gaming", (req,res)=>{
    const addingData = new User({
        genre: req.body.genre,
if (err = true){
    console.log("Document dubmited successfully");
    res.send("saved new data");

   module.exports = router;

I don’t know why its saying User is not defined because I exported router properly into the app.js by using module.exports = router. I think the module.export is not working properly and not bringing the code to app.js file. Thanks for the help


instead of

const User = mongoose.model('gaming', mySchema);

Try using

const User = (module.exports = mongoose.model('gaming', mySchema));;

Answered By – ps2708

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.