I did all the connections right but when I try to test it on postman it shows this error instead.
//my models file
const mongoose = require("mongoose")
const ProductSchema = new mongoose.Schema(
{
title: {type:String, required: [true,"{PATH} is required"] , minlength : [3, "{PATH} must be 3 or more"]},
price: {type:Number, required: [true,"{PATH} is required"], min : [0, "{PATH} must be 3 or more"]},
description: {type:String}
},{timestamps:true}
)
module.exports.Product = mongoose.model("Product",ProductSchema);
//my controller file
const Product = require("../models/product.model")
module.exports = {
getAll: (req,res) => {
Product.find()
.then(object=> res.json(object))
.catch(err=> res.json(err))
},
getOne: (req,res) => {
Product.findOne({_id: req.params.id})
.then(object=> res.json(object))
.catch(err=> res.json(err))
},
create: (req,res) => {
Product.create(req.body)
.then(object=> res.json(object))
.catch(err=> res.status(400).json(err))
},
update: (req,res) => {
Product.findByIdAndUpdate({_id: req.params.id}, req.body, {new:true, runValidators:true})
.then(object=> res.json(object))
.catch(err=> res.status(400).json(err))
},
delete: (req,res) => {
Product.deleteOne({_id: req.params.id})
.then(confirm=> res.json(confirm))
.catch(err=> res.json(err))
}
}
What should I do ? even when I tried it on getAll it says “TypeError: Product.find is not a function”
please help
New contributor
Ramez mezni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.