Home > Net >  trying to empty out the cookie and error happens
trying to empty out the cookie and error happens

Time:05-22

const logoutUser = (req, res) => {
  res
    .cookie("accessToken", "", { maxAge: 0, httpOnly: true })
    .cookie("refreshToken", "", { maxAge: 0, httpOnly: true })
    .status(200)
    .send({ thanks });
  console.log("hello");
};

this is the error that arises

CastError: Cast to ObjectId failed for value "logout" (type string) at path "_id" for model "user" at model.Query.exec (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/query.js:4715:21) at model.Query.Query.then (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/query.js:4814:15) at processTicksAndRejections (node:internal/process/task_queues:96:5) { messageFormat: undefined, stringValue: '"logout"', kind: 'ObjectId', value: 'logout', path: '_id', reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer at new BSONTypeError (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/bson/lib/error.js:41:28) at new ObjectId (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/bson/lib/objectid.js:65:23) at castObjectId (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/cast/objectid.js:24:12) at ObjectId.cast (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/schema/objectid.js:247:12) at ObjectId.SchemaType.applySetters (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/schematype.js:1189:12) at ObjectId.SchemaType._castForQuery (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/schematype.js:1623:15) at ObjectId.SchemaType.castForQuery (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/schematype.js:1613:15) at ObjectId.SchemaType.castForQueryWrapper (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/schematype.js:1590:20) at cast (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/cast.js:344:32) at model.Query.Query.cast (/media/esraa/C80C7B4B0C7B3412/token2/server/node_modules/mongoose/lib/query.js:5137:12), valueType: 'string' }

CodePudding user response:

With this code all ok. Somwhere else you trying assign String type to ObjectId type. This is the reason

Use special casting methods for this

  • Related