Home > Enterprise >  Check if the argument provided is a function & not a Mongoose model
Check if the argument provided is a function & not a Mongoose model

Time:07-12

I am writing a middleware. The middleware accepts two arguments, either a mongoose Model or a normal JavaScript function.

function someMiddleware(arg1){
   // here, how to differentiate?
   if(typeof arg1 === 'function') console.log("it's a function")
}

Mongoose models are functions, but I want the consumer of my someMiddleware function to be able to pass a model or a normal function.

But how can I differentiate between both in my code?

CodePudding user response:

// true for functions that don't inherit from mongoose.Model
Object.getPrototypeOf(arg1) !== mongoose.Model;
  • Related