When I tried to implement an express get service call with a mongoose find method it is giving me 'Product.find' is not a function error.
FYI-'Product' is the mongoose model which I imported .
const express = require('express');
const routes = express.Router();
const Product = require('../models/product');
routes.get('/',(req,res)=>{
const list= Product.find();
res.send(list);
})
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
name:String,
subject:String
})
exports.Product = mongoose.model('Product',productSchema);
TypeError:Product.find is not a function.
When you are exporting the model like exports.Product,you need to import the model in routes file as an object.
const {Product} = require('../models/product');
OR
we can use module.exports instead of exports.Product without making any changes in the import of routes file.
Because you are not importing your model in route file. Better if you can use module.exports instead of exports.Product.