我不知道为什么我会犯这个错误。每当我试图在我的电子商务项目网站上下订单。我得到了这个错误。
:5000/api/order:1
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
code: "ERR_BAD_RESPONSE"
config: {transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}
message: "Request failed with status code 500"
name: "AxiosError"
request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
response:
config: {transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}
data: {index: 0, code: 11000, keyPattern: {…}, keyValue: {…}}
headers: {content-length: '123', content-type: 'application/json; charset=utf-8'}
request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 500
statusText: "Internal Server Error"
[[Prototype]]: Object
[[Prototype]]: Error
这是我的useEffect请求。
useEffect(() => {
const createOrder = async () => {
try {
const res = await userRequest.post('/order', {
username: currentUser.username,
products: cart?.products.map((item) => ({
productId: item._id,
quantity: item._quantity,
})),
amount: cart?.total,
address: "India",
pincode: "110046",
phone: "1234567890",
email: "johndoe@xyz.com",
shippingPrice: "120",
reference: orderNo,
paymentMethod: "Cash on delivery",
status: "pending"
})
} catch (error) {
console.log(error)
}
}
createOrder()
}, [cart, currentUser])
这是userRequest函数。
const user = JSON.parse(localStorage.getItem("persist:root"))?.user;
const currentUser = user && JSON.parse(user).currentUser;
const TOKEN = currentUser?.accessToken;
const BASE_URL = "http://localhost:5000/api/";
export const userRequest = axios.create({
baseURL: BASE_URL,
headers: { token: `Bearer ${TOKEN}` },
});
这是我的添加顺序API。当我在邮递员上尝试它时,它工作得很好,但是每当我在一个项目上尝试它时,它都会给我一个错误。
const router = require("express").Router();
router.post("/", verifyToken, async (req, res) => {
const newOrder = new Order(req.body);
try {
const savedOrder = await newOrder.save();
res.status(200).json(savedOrder);
} catch (err) {
res.status(500).json(err);
}
});
这是订单模式。
const mongoose = require("mongoose");
const OrderSchema = new mongoose.Schema(
{
username: { type: String, required: true },
email: { type: String },
phone: { type: Number, required: true },
products: [
{
productId: {
type: String
},
quantity: {
type: Number,
default: 1,
},
},
],
amount: { type: Number, required: true },
shippingPrice: { type: Number, required: true },
address: { type: String, required: true },
pincode: { type: Number, required: true },
status: { type: String, default: "pending" },
trackingId: { type: String },
paymentMethod: { type: String, default: "Cash On Delivery" },
reference: { type: String, unique: true },
},
{ timestamps: true }
);
发布于 2022-08-18 09:49:19
这是最新消息。
我刚刚从mongoDB中删除了这个表,然后下了一个新的订单,现在它已经修好了。
https://stackoverflow.com/questions/73387008
复制相似问题