Normal
Normal

Reputation: 3676

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

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?

Upvotes: 0

Views: 28

Answers (1)

vkarpov15
vkarpov15

Reputation: 3872

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

Upvotes: 1

Related Questions