检查对象是否是mongo游标

时间:2015-04-01 16:27:19

标签: javascript mongodb meteor

我有一种方法可以接收列表或mongo游标并对其做出反应,例如:

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');
  }
}

然而,当我不期待它时,似乎会返回错误

> 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游标?

1 个答案:

答案 0 :(得分:1)

您应该可以使用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
相关问题