问题描述
首先,我是编程的真正初学者,我无法弄清楚这有什么问题 它只会显示未找到404错误
下面是我的猫鼬模式
const mongoose = require("mongoose");
const OrderSchema = new mongoose.Schema({
user_id: {
type: String,required: true,},order_date: {
type: Date,default: Date.now,total_items: {
type: Number,total: {
type: Number,name: {
type: String,address: {
type: String,postalcode: {
type: String,city: {
type: String,country: {
type: String,phonenumber: {
type: Number,order_item_status: {
//purchased item delivery status,added by constant at saving from controller nmethod
type: String,quantity_array: [
{
type: String,],purchase_array: [
{
type: String,item_array: [
{
type: String,});
module.exports = Order = mongoose.model("order",OrderSchema);
下面是我要发送的数据集。
{"user_id":"5f5058acc90b575a40d9e570","name":"Alfonso","address":"Victoria","postalcode":"11011B","city":"Moscow","country":"Russia","phonenumber":null,"total_items":2,"total":15,"quantity_array":[2,1],"purchase_array":["pb_single","pb_single"],"item_array":["5f55e91271b808206c132d7c","5f55e8e171b808206c132d7a"]}
及以下是我用来将上述对象发送到服务器的功能。
let orderComplete = {
user_id: localStorage.getItem("id"),name: this.state.name,address: this.state.address,postalcode: this.state.postalcode,city: this.state.city,country: this.state.country,phonenumber: this.state.phonenumber,total_items: this.state.totalitems,total: this.state.amount,quantity_array: this.state.quantityArray,purchase_array: this.state.purchaseTypeArray,item_array: this.state.comicIdArray,};
console.log("checkpoint : orderComplete" + JSON.stringify(orderComplete));
var jwt_token = localStorage.getItem("currentToken"); //because it is a protected route
const config = {
headers: {
"Content-Type": "application/json","x-auth-token": jwt_token,};
const body = JSON.stringify(orderComplete);
const result = await axios
.post("http://localhost:5000/createorder",body,config)
.then((res2) => {
console.log(res2);
toast.success(res2);
})
.catch((error) => {
toast.error(error);
console.log(error);
});
if (result == true) {
this.props.history.push("/products");
toast.success("Order Success");
}
}
当从服务器端捕获时 功能如下
const createOrder = async (req,res) => {
try {
let user_id = req.body.user_id;
let purchase_type = req.body.purchase_type;
let item_array = req.body.item_array;
let quantity_array = req.body.quantity_array;
let purchase_array = req.body.purchase_array;
let total_items = req.body.total_items;
let total = req.body.total;
let name = req.body.name;
let address = req.body.address;
let postalcode = req.body.postalcode;
let city = req.body.city;
let country = req.body.country;
let phonenumber = req.body.phonenumber;
let order_item_status = "processing";
if (
purchase_type == false ||
user_id == false ||
item_array == false ||
quantity_array == false ||
purchase_array == false ||
total_items == false ||
total == false ||
name == false ||
address == false ||
postalcode == false ||
city == false ||
country == false ||
phonenumber == false
) {
return res.status(400).send("Not all mandatory values have been set!");
}
OrderObject = new Order({
//create object to set data
purchase_type,user_id,item_array,quantity_array,purchase_array,total_items,total,name,address,postalcode,city,country,phonenumber,order_item_status,});
await OrderObject.save();
res.status(200).send("Order success");
} catch (error) {
console.log("Error caught at ",error);
res.status(500).send("Server Error");
}
};
我一遍又一遍地尝试,但是我无法弄清楚到底出了什么问题,即使异常处理程序也没有包含很多信息。
我非常需要您的帮助,谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)