OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I keep getting this error when I run it on postman : "TypeError: Product.create is not a function"

  • Thread starter Thread starter Ramez mezni
  • Start date Start date
R

Ramez mezni

Guest
I did all the connections right but when I try to test it on postman it shows this error instead.

Code:
//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);

Code:
//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

<p>I did all the connections right but when I try to test it on postman it shows this error instead.</p>
<pre><code>//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);
</code></pre>
<pre><code>//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))
}
}
</code></pre>
<p>What should I do ? even when I tried it on getAll it says "TypeError: Product.find is not a function"
please help</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top