Home > Enterprise >  CastError : try implement a route and right away get error
CastError : try implement a route and right away get error

Time:11-18

i do a project in nodejs with express and mongo now i tried do implement a simple route but i get an error

//this is the route
router.patch('/resetPassword/:token', authController.resetPassword)


//this is the resetPassword function
exports.resetPassword = (req, res, next) => {
    console.log("work")
}

But as soon as I click on the request I get the error even the consul.log is not read

C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:4498
  const castError = new CastError();
                    ^

CastError: Cast to ObjectId failed for value "resetPassword" (type string) at path "_id" for model "User"
    at model.Query.exec (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:4498:21)
    at model.Query.Query.then (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:4592:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  messageFormat: undefined,
  stringValue: '"resetPassword"',
  kind: 'ObjectId',
  value: 'resetPassword',
  path: '_id',
  reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
      at new ObjectID (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\bson\lib\bson\objectid.js:59:11)
      at castObjectId (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\cast\objectid.js:25:12)
      at ObjectId.cast (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\schema\objectid.js:246:12)
      at ObjectId.SchemaType.applySetters (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\schematype.js:1123:12)
      at ObjectId.SchemaType._castForQuery (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\schematype.js:1601:15)
      at ObjectId.SchemaType.castForQuery (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\schematype.js:1591:15)
      at ObjectId.SchemaType.castForQueryWrapper (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\schematype.js:1568:20)
      at cast (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\cast.js:332:32)     
      at model.Query.Query.cast (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:4937:12)
      at castQuery (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:4738:18)
      at model.Query.Query._findAndModify (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:3598:23)
      at model.Query.<anonymous> (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\query.js:3164:8)
      at model.Query._wrappedThunk [as _findOneAndUpdate] (C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8)
      at C:\Users\5MATA\Desktop\first-real-project\BOOKS-PROJECT\BOOKS-BACK\node_modules\kareem\index.js:370:33
      at processTicksAndRejections (node:internal/process/task_queues:78:11),
  valueType: 'string'
}

CodePudding user response:

This is how you do it.

route.js

const router = require("express").Router();
const adminCtrl = require("./adminCtrl");

router.post("/login", adminCtrl.adminLogin);

adminCtrl.js

exports.adminLogin = (req, res,next) => {

}

CodePudding user response:

Can you please elaborate more on the things you are doing with the resetPassword function? or before it.

The error

    `Cast to ObjectId failed for value "resetPassword"`

is a mongoose error that occurs when we try to find a "_id" in mongo using an invalid string.

  • Related