corvid
corvid

Reputation: 11187

check if object is a mongo cursor

I have a method in which I want to either receive a list or a mongo cursor and react to that, for example:

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

However, it seems to return false when I don't expect it

> 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

How can I tell if an object is a Mongo cursor?

Upvotes: 1

Views: 594

Answers (1)

Carson Moore
Carson Moore

Reputation: 1287

You should be able to use instanceof Mongo.Collection.Cursor (not Mongo.Cursor). From my console:

> a = Meteor.users.find()
<- LocalCollection.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: Minimongo.Matcher, skip: undefined…}
> a instanceof Mongo.Collection.Cursor
<- true

Upvotes: 1

Related Questions