I have a React+Node Web app, and I cant’t figure out why the delete is not working, Create, Read, Update work well, I have tested with Postman
**The error I get is:(put the one from the server)
SQLITE_ERROR: no such table: main.product
the error in server terminal
This is my delete function from my controller
exports.deleteProduct = async (req, res) => {
try {
const products = await Product.findByPk(req.params.id);
if (!products) {
res.status(404).send("Product not found");
return;
}
if (products.imageUrl) {
const imagePath = path.join(__dirname, "..", products.imageUrl);
fs.unlink(imagePath, (err) => {
if (err) {
console.error("Failed to delete the image file:", err);
}
});
}
await products.destroy();
res.status(204).send("Product deleted");
} catch (error) {
console.error("Failed to delete product:", error);
res.status(500).send(error.message);
}
};
This is my Product Model
const { Model, DataTypes } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Product extends Model {}
Product.init(
{
name: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
price: {
type: DataTypes.FLOAT,
allowNull: false,
validate: {
isFloat: true,
},
},
category: {
type: DataTypes.STRING,
allowNull: false,
},
imageUrl: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
sequelize,
modelName: "Product",
tableName: "products",
freezeTableName: true,
timestamps: false,
}
);
return Product;
};
And here are the routes
``const express = require("express");
const router = express.Router();
const productController = require("../controllers/productController");
const upload = require("../config/uploadConfig");
router.get("/", productController.getAllProducts);
router.post("/add", upload.single("image"), productController.createProduct);
router.get("/:id", productController.getProductById);
router.put("/:id", productController.updateProduct);
router.delete("/:id", productController.deleteProduct);
module.exports = router;`
`
The Delete Function
Postman Request
I did rewrite the functions multiple times, i remaked the Model, I can’t wrap my mind around the fact that it says the table isnt found but the other functions work…
New contributor
user25266186 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.