簡體   English   中英

流星:檢查是否定義了集合

[英]Meteor: Check if collection is defined

有一個復雜的方法函數,該函數從不同的集合中獲取一些數據。 這些集合在某些(可選)程序包中定義。 現在,我需要檢查集合是否已定義-這意味着該包已添加到項目中。

我嘗試使用if ,但這是行不通的。 我仍然收到錯誤“ Articles is not defined並且腳本中止。

Meteor.methods({
    data: function () {

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

使用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> 

有幾種方法可以執行此操作,具體取決於集合的定義方式。 假設它是一個全局變量,您應該可以像這樣安全地對其進行測試:

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

另請參閱參考資料集

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM