Home > Mobile >  Trying to convert id prop to mongoose ObjectId
Trying to convert id prop to mongoose ObjectId

Time:08-09

this is my function:

 acceptContactRequest: async (
      _: any,
      { userId }: IUserInfo,
      context: ExpressContext
    ) => {.....}

IUserInfo:

export interface IUserInfo {
  username: string;
  password: string;
  userId: mongoose.Schema.Types.ObjectId;
}

this is what I am trying to do:

   userAuth?.pendingRequests.filter(
        (requestId) => userId !== requestId
      );

requestId output is new ObjectID(id)

and userId output is only the id.

I tried to convert it like this :

 userAuth?.pendingRequests.filter(
        (requestId) => mongoose.Types.ObjectId(userId) !== requestId
      );

  userAuth?.pendingRequests.filter(
        (requestId) => ObjectId(userId) !== requestId
      );
  userAuth?.pendingRequests.filter(
        (requestId) => new ObjectId(userId) !== requestId
      );

but none of them is work. I always get this error:

ObjectId' only refers to a type, but is being used as a value here

Thank you!

Edit: requestId is coming from here enter image description here

I want to match that id with userId..

CodePudding user response:

You can not directly use ObjectId s for comparison, have to convert into string.

import { Types } from 'mongoose'

export interface IUserInfo {
  username: string
  password: string
  userId: Types.ObjectId
}

// assuming both `requestId` and `userId` here are ObjectId
userAuth?.pendingRequests.filter((requestId) => ''   userId  !== ''   requestId)

  • Related