英文:
Why am I getting error while sending PUT request to the server?
问题
我已经为创建我的端点创建了两个文件,分别是productController和productRouter。发布和获取(所有产品)功能正常工作。但删除、更新和获取(单个产品)功能不起作用。在Postman上只显示HTML代码。以下是我的productController:
const Product = require("../models/productModel");
// 创建产品 --管理员
exports.createProduct = async (req, res, next) => {
const product = await Product.create(req.body);
res.status(201).json({
success: true,
product
})
}
// 获取所有产品
exports.getAllProducts = async (req, res) => {
const products = await Product.find();
res.status(200).json({ success: true, products })
}
// 获取产品详情
exports.getProduct = async (productId) => {
const product = await Product.findById(productId);
if (!product) {
return res.status(500).json({ success: false, message: "未找到" })
}
return product
}
// 更新产品 --管理员
exports.updateProduct = async (req, res, next) => {
let product = await Product.findById(req.params.id);
if (!product) {
return res.status(500).json({ success: false, message: "未找到" })
}
product = await Product.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
useFindAndModify: false,
});
res.status(200).json({
success: true,
product,
});
}; // 不起作用
// 删除产品
exports.deleteProduct = async (productId) => {
var data = await Product.findByIdAndDelete(productId)
return data
}
以下是我的productRouter:
const express = require("express");
const { getAllProducts, createProduct, updateProduct, deleteProduct, getProduct } = require("../controllers/productController");
const router = express.Router();
router.route("/products").get(getAllProducts)
router.route("/product/new").post(createProduct)
router.route("/product/:id").put(updateProduct)
router.delete("/product/:id", async (req, res) => {
var { id } = req.params
res.json(await deleteProduct(id))
});
router.get("/product/:id", async (req, res) => {
var { id } = req.params;
var products = await getProduct(id);
res.json(products);
});
module.exports = router
我只在后端工作,尚未开始前端工作。
英文:
I have created two files namely productController and productRouter for creating my endpoints. Post and get(all products) are working well. But delete, put, and get(single product) are not working. Only Html code is displayed on postman. here is my productController
const Product = require("../models/productModel");
//create product --admin
exports.createProduct = async (req,res,next)=>{
const product = await Product.create(req.body);
res.status(201).json({
success:true,
product
})
}
//get All Products
exports.getAllProducts = async (req,res)=>{
const products = await Product.find();
res.status(200).json({success:true,products})
}
//get product details
exports.getProduct = async(productId)=>{
const product = await Product.findById(productId);
if(!product){
return res.status(500).json({success:false,message:"Not found"})
}
return product
}
//update product --admin
exports.updateProduct = async (req, res, next) => {
let product = await Product.findById(req.params.id);
if(!product){
return res.status(500).json({success:false,message:"Not found"})
}
product = await Product.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
useFindAndModify: false,
});
res.status(200).json({
success: true,
product,
});
}; //not working
//delete
exports.deleteProduct = async(productId)=>{
var data=await Product.findByIdAndDelete(productId)
return data
}
here is my productRouter
const express = require("express");
const { getAllProducts,createProduct, updateProduct, deleteProduct, getProduct } = require("../controllers/productController");
const router = express.Router();
router.route("/products").get(getAllProducts)
router.route("/product/new").post(createProduct)
router.route("product/:id").put(updateProduct)
router.delete("/product/:id",async(req,res)=>{
var {id}=req.params
res.json(await deleteProduct(id))
});
router.get("/product/:id", async (req, res) => {
var { id } = req.params;
var products = await getProduct(id);
res.json(products);
});
module.exports = router
I am working on backend only, haven't started with frontend
your text
答案1
得分: 1
如果你遇到类似于“无法 PUT”的错误,意味着你正在发送请求到一个不存在的 API。
在查看了你的代码后,我发现你在 PUT 请求的开头缺少了斜杠。
只需添加斜杠,它就会工作。
router.route("/product/:id").put(updateProduct)
英文:
If you are getting the error like Cannot PUT
, it means that you are sending the request on an api which doesn't exist.
After looking at your code, i found that you are missing the slash in the beginning of put request.
Just add the slash and it will work.
router.route("/product/:id").put(updateProduct)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论