微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

只想向在 mongodb MERN STACK DEVELOPMENT 中已下订单的特定产品所有者显示订单

如何解决只想向在 mongodb MERN STACK DEVELOPMENT 中已下订单的特定产品所有者显示订单

这是我的订单架构 实际下单的人,可以拿到他的特定订单,但是拿到他产品订单的人不能拿到具体订单。哪个是他的,我正在复制教程并制作产品,请帮忙

const mongoose = require('mongoose')

const orderSchema = mongoose.Schema({
    shippingInfo: {
        address: {
            type: String,required: true
        },city: {
            type: String,phoneNo: {
            type: String,postalCode: {
            type: String,country: {
            type: String,required: true
        }
    },user: {
        type: mongoose.Schema.Types.ObjectId,required: true,ref: 'User'
    },orderItems: [
        {
            name: {
                type: String,required: true
            },quantity: {
                type: Number,image: {
                type: String,price: {
                type: Number,product: {
                type: mongoose.Schema.Types.ObjectId,ref: 'Product'
            }
        }
    ],paymentInfo: {
        id: {
            type: String
        },status: {
            type: String
        }
    },paidAt: {
        type: Date
    },itemsPrice: {
        type: Number,default: 0.0
    },taxPrice: {
        type: Number,shippingPrice: {
        type: Number,totalPrice: {
        type: Number,orderStatus: {
        type: String,default: 'Processing'
    },deliveredAt: {
        type: Date
    },createdAt: {
        type: Date,default: Date.Now
    }

})

module.exports = mongoose.model('Order',orderSchema)

这是我的产品架构


const mongoose = require('mongoose')

const productSchema = new mongoose.Schema({
    name: {
        type: String,required: [true,'Please enter product name'],trim: true,maxLength: [100,'Product name cannot exceed 100 characters']
    },price: {
        type: Number,'Please enter product price'],maxLength: [5,'Product name cannot exceed 5 characters'],description: {
        type: String,'Please enter product description'],},ratings: {
        type: Number,default: 0
    },images: [
        {
            public_id: {
                type: String,url: {
                type: String,}
    ],category: {
        type: String,'Please select category for this product'],enum: {
            values: [
                'Electronics','Cameras','Laptops','Accessories','Headphones','Food',"Books",'Clothes/Shoes','Beauty/Health','Sports','Outdoor','Home'
            ],message: 'Please select correct category for product'
        }
    },seller: {
        type: String,'Please enter product seller']
    },stock: {
        type: Number,'Please enter product stock'],numOfReviews: {
        type: Number,reviews: [
        {
            user: {
                type: mongoose.Schema.ObjectId,ref: 'User',name: {
                type: String,rating: {
                type: Number,comment: {
                type: String,required: true
            }
        }
    ],user: {
        type: mongoose.Schema.ObjectId,required: true
    },default: Date.Now
    }
})

module.exports = mongoose.model('Product',productSchema);

这些是我的订单控制器功能

  exports.myOrders = catchAsyncErrors(async (req,res,next) => {
    const orders = await Order.find({ user: req.user.id })

    res.status(200).json({
        success: true,orders
    })
})


// Get all orders - ADMIN  =>   /api/v1/admin/orders/
exports.allOrders = catchAsyncErrors(async (req,next) => {
    const orders = await Order.find({ user: req.user.id })

    //let totalAmount = 0;

   /*  orders.forEach(order => {
        totalAmount += order.totalPrice
    })
 */
    res.status(200).json({
        success: true,//  totalAmount,orders
    })
})

发起订单的用户可以看到自己发起的订单,但被下单的人无法得到具体的订单 我希望我说得足够清楚,请帮助我我是菜鸟

解决方法

对于用户,您可以使用 Model.find({ user: user_id });

对于卖家,您应该使用Model.find({ user: user_id,seller: seller_id })

这将匹配来自用户的订单给卖方

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。