如何解决获取用户订单时遇到问题我正在使用 Mongo DB,我可以看到 DB 中的所有订单我如何获得代码
当我从 Postman 运行代码并且在我的前端时,我收到此错误并且无法访问用户订单。我已经创建了模型和控制器并将模型调用到 cronrollers 文件中。任何人都知道我该如何解决这个问题?
"message": "Resources not found:_id","error": "CastError: Cast to ObjectId Failed for value \"personalOrders\" at path \"_id\" for model \"Orders\"\n at model.Query.exec (D:\\Webapp\\node_modules\\mongoose\\lib\\query.js:4360:21)\n at model.Query.Query.then (D:\\Webapp\\node_modules\\mongoose\\lib\\query.js:4454:15)\n at processticksAndRejections (internal/process/task_queues.js:97:5)"
}
有人知道我如何解决这个问题吗?
这是代码
.订单控制器文件
personalOrders: asyncErrorHandler(async (req,res,next) => {
const orders = Orders.find({users: req.users.id})
res.status(200).json({
success: true,orders
})
}),
订单模型
const mongoose = require('mongoose')
const ordeRSSchema = new mongoose.Schema({
users: {
type: mongoose.Schema.Types.ObjectId,required: true,ref: 'users'
},purchaseItems: [
{
title: {
type: String,required: true
},quantity: {
type: Number,images: {
type: String,price: {
type: Number,products: {
type: mongoose.Schema.Types.ObjectId,ref: 'Products'
},}
],orderStatus: {
type: String,default: 'Processing',required: true
},deliveredTime: {
type: Date
},createdTime: {
type: Date,default: Date.Now
},paymentData: {
id: {
type:String
},status: {
type:String
}
},productPrice: {
type: Number,default: true,shippingPrice: {
type: Number,taxPrice: {
type: Number,sumPrice: {
type: Number,shippingData: {
address: {
type: String,required: true
},city: {
type: String,phoneNumber: {
type: String,postCode: {
type: String,country: {
type: String,required: true
}
},})
module.exports = mongoose.model('Orders',ordeRSSchema)
订购路线
router.get('/orders/personalOrders',isUserAuth,personalOrders)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。