Next.js Discord

Discord Forum

Need help with product.remove or Delete function on my website

Unanswered
Tomistoma posted this in #help-forum
Open in Discord
TomistomaOP
whenever is click on Delete product it returns error 500 internal server error

this is the console error:
error pages/api/admin/products/[id]/index.js (51:20) @ remove
- error Error [TypeError]: product.remove is not a function
at deleteHandler (webpack-internal:///(api)/./pages/api/admin/products/[id]/index.js:65:23)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
digest: undefined
}
49 | const product = await Product.findById(req.query.id);
50 | if (product) {
51 | await product.remove();
| ^
52 | await db.disconnect();
53 | res.send({ message: 'Product deleted successfully' });
54 | } else {

If there are any more code you need please tell me i cant post them all at once

3 Replies

TomistomaOP
this is the pages/api/admin/products/[id]/index.js page
import { getToken } from 'next-auth/jwt';
import Product from '../../../../../models/Product';
import db from '../../../../../utils/db';

const handler = async (req, res) => {
const user = await getToken({ req });
if (!user || (user && !user.isAdmin)) {
return res.status(401).send('signin required');
}

if (req.method === 'GET') {
return getHandler(req, res, user);
} else if (req.method === 'PUT') {
return putHandler(req, res, user);
} else if (req.method === 'DELETE') {
return deleteHandler(req, res, user);
} else {
return res.status(400).send({ message: 'Method not allowed' });
}
};
const getHandler = async (req, res) => {
await db.connect();
const product = await Product.findById(req.query.id);
await db.disconnect();
res.send(product);
};
const putHandler = async (req, res) => {
await db.connect();
const product = await Product.findById(req.query.id);
if (product) {
product.name = req.body.name;
product.slug = req.body.slug;
product.price = req.body.price;
product.category = req.body.category;
product.image = req.body.image;
product.brand = req.body.brand;
product.countInStock = req.body.countInStock;
product.description = req.body.description;
await product.save();
await db.disconnect();
res.send({ message: 'Product updated successfully' });
} else {
await db.disconnect();
res.status(404).send({ message: 'Product not found' });
}
};
const deleteHandler = async (req, res) => {
await db.connect();
const product = await Product.findById(req.query.id);
if (product) {
await product.remove();
await db.disconnect();
res.send({ message: 'Product deleted successfully' });
} else {
await db.disconnect();
res.status(404).send({ message: 'Product not found' });
}
};
export default handler;
this is the Product.js model

import mongoose from "mongoose";

const productSchema = new mongoose.Schema(
{
name: { type: String, required: true},
slug: { type: String, required: true, unique: true },
category: { type: String, required: true },
image: { type: String, required: true },
price: { type: Number, required: true },
brand: { type: String, required: true },
rating: { type: Number, required: true , default: 0 },
numReviews: { type: Number, required: true, default: 0 },
countInStock: { type: Number, required: true, default: 0 },
description: { type: String, required: true },
},
{
timestamps: true,
}
);

const Product = mongoose.models.Product || mongoose.model("Product", productSchema);
export default Product;
could this be a mongoose error?