猫鼬:检查对象是否是猫鼬对象 [英] Mongoose: check if object is mongoose object

查看:55
本文介绍了猫鼬:检查对象是否是猫鼬对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道检查一个对象是否是猫鼬对象的最简单方法是什么?我只是最好检查是否定义了 toObject() 或者是否有更有效的方法.非常感谢

anyone know what the simplest way to check whether an object is a mongoose object? Am I just best checking if toObject() is defined or is there a more efficient way. many thanks

推荐答案

您可以通过 instanceof 运算符检查对象的原型,以确认它是您的猫鼬模型的实例.使用来自 mongoosejs.com 的示例架构:

You can check the object's prototype via the instanceof operator to confirm it's an instance of your mongoose model. Using the example schema from mongoosejs.com:

if (obj instanceof Cat) {
    // yes, it's a mongoose Cat model object
    ...
}

这篇关于猫鼬:检查对象是否是猫鼬对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆