我有一个方法,我想接收列表或 mongo 游标并对此使用react,例如:
createFromTemplate: function(template) {
var iter;
if(template instanceof Mongo.Cursor) {
iter = template.fetch();
} else if(template instanceof Array) {
iter = template;
} else {
throw new Meteor.Error(500, 'Template must be a Cursor or Array');
}
}
但是,当我没有预料到时,它似乎返回 false
> var p = PageTemplates.find(); // as a mongo cursor
> var pArray = p.fetch(); // as an array
> Object.prototype.toString.call(p);
[object Object]
> typeof p
Object
> p instanceof Mongo.Cursor
false
如何判断一个对象是否是 Mongo 游标?
最佳答案
您应该能够使用instanceof Mongo.Collection.Cursor
(而不是Mongo.Cursor
)。从我的控制台:
> a = Meteor.users.find()
<- LocalCollection.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: Minimongo.Matcher, skip: undefined…}
> a instanceof Mongo.Collection.Cursor
<- true
关于javascript - 检查对象是否是 mongo 游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29396038/