简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM