2015-04-01 22 views
1

我有,我想要麼接受列表或蒙戈光標並作出反應的是一個方法,例如:檢查,如果對象是一個蒙戈光標

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遊標?

回答

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