Reference error while runing an api in node js. I have created the model and required it also. But still not working

Viewed 44

I have a node.js model which shown here:

    const mongoose = require('mongoose');
    const homeSchema =new mongoose.Schema({
      _id : mongoose.Schema.Types.ObjectId,
      name : String,
      address : String,
      ownername : String
    });

module.exports = mongoose.model('Home', homeSchema);

which I'm using in my route file:

const express = require('express');
const mongoose = require('mongoose');
const HomeSchema = require('../models/home');
const router = express.Router();

router.get('/post',(req,res)=>{
  res.status(200).json({message:'Get method'});
});


    router.post('/add',(req,res)=>{
      const home = new Home({
        _id : mongoose.Types.ObjectId(),
        name : 'Name',
        address : 'Kanter',
        ownername : 'data'
      });
      home.save().then(result=>{
        res.status(200).json(result);
      });

    });

    module.exports = router;

But when I'm hitting the API, I'm getting this error:

ReferenceError: Home is not defined

1 Answers

you are trying to call new Module that is not defined.

const HomeSchema = require('../models/home');

should be

const Home = require('../models/home');
Related