javascript - 检查对象是否是 mongo 游标

标签 javascript mongodb meteor

我有一个方法,我想接收列表或 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/

相关文章:

javascript - 在 jQuery ajax 函数中使用 jQuery 插件的范围问题

javascript - Meteor onRendered - 访问 DOM 困难

javascript - 在 meteor 中使用 Chart.js 我的图表未显示在浏览器屏幕上

javascript - 从其他 React 组件访问函数

javascript - 当 Canvas 重新定位时,Fabric.js Canvas 坐标的值会发生变化

javascript - 如何将元素从外部移动/拖动到 iframe

mongodb - 无法通过 2sphere 找到元素

javascript - 只要找到相似的条目,它就应该增加

mongodb - 如何在 Mac OSX 上将 mongo 命令添加到 PATH

javascript - 如何在 Javascript 中创建一个 div 并将其定位在 html 模板的中间