流星:检查是否定义了集合

时间:2016-03-16 19:54:45

标签: javascript mongodb meteor

有一个复杂的方法函数,它从不同的集合中获取一些数据。这些集合在一些(可选)包中定义。 现在我需要检查集合是否已定义 - 这意味着该包已添加到项目中。

我尝试使用if,但这不起作用。我仍然收到错误Articles is not defined并且脚本中止。

Meteor.methods({
    data: function () {

        if (Articles) {
            Articles.find(
                { parent: null }, 
                { fields: { title: true } } 
            );
        }
    }
});

2 个答案:

答案 0 :(得分:0)

使用typeof检查它是否为undefined



'use strict'; // Just to make sure we would get a reference error

if (typeof Articles === 'undefined') {
  document.querySelector('pre').innerText = 'No articles here';
}

<pre>Articles?</pre>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据集合的定义方式,有几种方法可以做到这一点。假设它是一个全局变量,你应该能够安全地测试它,如下所示:

var root = Meteor.isClient ? window : global;
if (root.Articles) {...}

另见collections by reference

相关问题