簡體   English   中英

如何等待Meteor.user()在Template onRendered中定義

[英]How to wait for Meteor.user() to be defined in Template onRendered

我正在研究Meteor項目,我需要訪問存儲在用戶個人資料中的一些信息才能在頁面上繪制地圖。 但是,當我嘗試訪問Meteor.user()時,收到未定義的信息,因為調用該函數時,尚未加載Meteor.user()。

Template.body.onRendered () ->
    console.log Meteor.user()
    address = Meteor.user()['profile']['address']

    GoogleMaps.ready 'studyMap', (map) ->
        # maps code that relies on address

由於未定義Meteor.user(),因此無法使地圖正常工作。 如何等待Meteor.user()定義?

我認為您僅在address = Meteor.user()['profile']['address']加載時才需要異步回調來加載地圖。 至於如何實現這一點,我不確定。

我的一個應用程序必須執行類似操作才能等待訂閱。 我在路由器中定義了waitOn()處理函數。

Router.map(function(){
  this.route('Brocator', {
    path: '/',
    template: 'brocator',
    waitOn: function() {
      return (Meteor.subscribe('people', this.params._gridId)
        && Meteor.subscribe('gridlog', this.params._gridId) );
    },
    data: function() {
      var gridId = this.params._gridId;
      return (People.find({gridId: gridId})
        && GridLog.find({gridId: gridId}));
    },
    fastRender: true
  }),
  ... other routes deleted for clarity
})

您可能決定按照以下方式使用某些東西:

return Meteor.user() != 'undefined';

在您的waitOn()處理函數中。

在這里發布流星/反應的答案,但是同樣可以大放異彩。 我對火焰並不熟悉,但是在Template.body.onRendered函數中,您可以偵聽以確保在呈現任何內容之前已配置登錄服務。

Tracker.autorun(() => {
  if (Accounts.loginServicesConfigured()) {
    // accounts is ready go ahead and render
  } else {
    // still waiting show loading spinner
  }
});

我建議將此邏輯提取到更高的級別,以免您重復執行相同的代碼。

暫無
暫無

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

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