Home > OS >  Not able to get user id
Not able to get user id

Time:12-30

I want to get orders of logged in user!

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

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

But at execution it says: "Resource not found. Invalid: _id".

This is the output:

{
  "success": true,
  "user": {
    "avatar": {
      "public_id": "This is a sample id",
      "url": "Temporary Url"
    },
    "_id": "61b1c9b50192723616d8c358",
    "name": "Vipin",
    "email": "[email protected]",
    "password": "$2a$12$A69H47oxL8USFgvtk0.8pOvu57BJ3p67QRtjrf5z35GNVOCNN1936",
    "role": "user",
    "__v": 0,
    "createdAt": "2021-12-29T08:14:46.321Z"
  },

This is what I get after placing order

{
  "success": true,
  "order": {
    "orderItems": [
      {
        "name": "Product3",
        "price": 1223,
        "quantity": 1,
        "image": "Sample Image",
        "product": "61ac9638b9ea9468971a4e43",
        "_id": "61cc1c0ba569e690850a9c89"
      }
    ],
    "user": "61b1c9b50192723616d8c358",
    "paymentInfo": {
      "id": "Sample Payment",
      "status": "Done"
    },
    
    "_id": "61cc1c0ba569e690850a9c88",
    
    "__v": 0
  }
}

Can any one help!!

Thank's in advance.

CodePudding user response:

try this

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

if(!ObjectId.isValid(req.user._id)) throw new Error(`Invalid id provided ${req.user._id}`);

{ user: ObjectId(req.user._id) }

CodePudding user response:

I got the problem. I have created two routes with same path that's why it was not working. Thanks to those who tried to help.

  • Related