I was trying to fetch my orders from my database for my logged In user but when I fetched it I am getting error from database, And Here is my code:-
// @desc Get logged in user orders
// @route GET /api/orders/myorders
// @access Private
const getMyOrders = asyncHandler(async (req, res) => {
const orders = await Order.find({ user: req.user._id })
res.json(orders);
})
Here is my route file:-
import express from 'express';
import { addOrderItems, getMyOrders, getOrderById, updateOrderToPaid } from '../controllers/orderController.js';
import { protect } from '../middleware/authMiddleware.js';
const router = express.Router();
router.route('/').post(protect, addOrderItems)
router.route('/:id').get(protect, getOrderById)
router.route('/:id/pay').put(protect, updateOrderToPaid)
router.route('/myorders').get(protect, getMyOrders)
export default router;
Here is my Order model file
import mongoose from 'mongoose';
const orderSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
orderItems: [
{
name: { type: String, required: true },
qty: { type: Number, required: true },
image: { type: String, required: true },
price: { type: Number, required: true },
product: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Product'
}
}
],
shippingAddress: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
country: { type: String, required: true }
},
paymentMethod: {
type: String,
required: true
},
paymentResult: {
id: { type: String },
status: { type: String },
update_time: { type: String },
email_address: { type: String }
},
taxPrice: {
type: String,
required: true,
default: 0.0
},
shippingPrice: {
type: String,
required: true,
default: 0.0
},
totalPrice: {
type: String,
required: true,
default: 0.0
},
isPaid: {
type: Boolean,
required: true,
default: false
},
paidAt: {
type: Date
},
isDelivered: {
type: Boolean,
required: true,
default: false
},
deliveredAt: {
type: Date
}
}, {
timeStamps: true
})
const Order = mongoose.model('Order', orderSchema)
export default Order;
And I also upload my protect middleware where I add my protect route and from where i get the user
import jwt from 'jsonwebtoken';
import asyncHandler from 'express-async-handler';
import User from '../models/userModel.js';
const protect = asyncHandler(async (req, res, next) => {
let token;
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) {
try {
token = req.headers.authorization.split(' ')[1]
const decoded = jwt.verify(token, process.env.JWT_SECRET)
req.user = await User.findById(decoded.id).select('-password')
next()
} catch (error) {
console.log(error)
res.status(401)
throw new Error('Not authorized, token not found')
}
}
if (!token) {
res.status(401)
throw new Error('Not authorized')
}
})
export { protect }
And My error:-
{
"message": "Cast to ObjectId failed for value \"myorders\" at path \"_id\" for model \"Order\"",
"stack": "CastError: Cast to ObjectId failed for value \"myorders\" at path \"_id\" for model \"Order\"\n at model.Query.exec (C:\\proShop\\node_modules\\mongoose\\lib\\query.js:4358:21)\n at model.Query.Query.then (C:\\proShop\\node_modules\\mongoose\\lib\\query.js:4450:15)\n at processTicksAndRejections (internal/process/task_queues.js:93:5)"
}
I uploaded my all files and code I anyone get knows the solution please tell me, please.