2017-08-10 102 views
0

我正在通過Meteor.call()函數從我的客戶端代碼調用服務器方法。如何從客戶端的Meteor.call方法返回結果?

但是當我試圖將回調函數的返回值傳遞給onCreated函數中的變量時,我得到一個未定義的值。

望着這個解決方案的結果仍然只是回調的範圍內,可供選擇:

https://stackoverflow.com/a/31661065/1829251

此外,當我閱讀文檔的流星稱之爲解釋說,該方法是異步這解釋該函數的返回值不能分配給一個變量:

https://docs.meteor.com/api/methods.html#Meteor-call

問題:

如何從客戶端的Meteor.call方法返回結果?的客戶端代碼

代碼片段:

import { Template } from 'meteor/templating'; 
import { Meteor } from 'meteor/meteor'; 
import { Datasets } from '../../../../api/datasets/datasets.js'; 
import { Constants } from '../../../../startup/constants.js'; 


import './rollup-history.html'; 
import './rollup-history.scss'; 

Template.rollup_history.onCreated(function() { 


    this.historyDays = 7; 


    this.rollupHistoryMECTitles =() => { 

    let mecObjects = []; 

    // Query the MEC calendar dates from AppConfiguration collection 
    let method = Constants.Methods.getAppConfigCollectionFromKeyDateSorted; 
    mecObjects = Meteor.call(method, Constants.AppConfigKeys.meccalendarInitialValues, this.historyDays, (error, result) => { 
      if(error){ 
      console.log("Error retrieving MEC Dates" + error); 
      } 

      //Logging values here shows the values as expected 
      console.log("MEC Dates in View" + JSON.stringify(result)); 
      return result; 
     } 
    ); 

    //logging value here shows undefined value 
    console.log("mec objects: " + JSON.stringify(mecObjects)); 

    return mecObjects; 


    }; 


}); 

Template.rollup_history.helpers({ 



    mecHeaderColumns: function() { 

    return Template.instance().rollupHistoryMECTitles(); 
    }, 

}); 
+0

而是你嘗試創建一個模板反應變種。將'Meteor.call'的函數內的變量賦值爲'Template.instance()。x.set(result)' –

+0

你究竟在做什麼?從onCreated()返回一個值對你來說意味着什麼?即使沒有方法調用,這對我來說也沒有意義。 – zim

回答

1

的價值你mecObjects永遠是undefined,因爲Meteor.call功能不返回任何東西。服務器的方法調用響應(或錯誤)將僅傳遞給回調函數,實際上就像代碼中那樣:errorresult變量。

0

簡短的回答:

定義通用代碼(代碼提供給服務器和客戶端)內的Meteor.Method然後使用Meteor.apply與選項{ returnStubValue : true }

mecObjects = Meteor.apply(method, [Constants.AppConfigKeys.meccalendarInitialValues, this.historyDays], { returnStubValue: true }, (error, result) => { 
     if(error){ 
     console.log("Error retrieving MEC Dates" + error); 
     } 

     //Logging values here shows the values as expected 
     console.log("MEC Dates in View" + JSON.stringify(result)); 
     return result; 
    } 
); 

console.log(mecObjects) //prints the client-side result of the meteor method 

不再回答:

流星允許用於模擬瀏覽器上的服務器方法的Optimistic-UI。要啓用此功能,請在通用代碼(服務器+客戶端空間)中定義您的Meteor.Method。這很有用,因爲用戶可以更快地看到UI更新,因爲我們不需要進行服務器往返。 meteor method flow

請注意,在上圖中,方法調用首先在客戶機上運行,​​然後在服務器上運行。 { returnStubValue: true }允許呼叫者客戶端運行

接收方法的返回值當編寫一個流星的方法,你可以使用this.isSimulation指定什麼邏輯完全運行在客戶端或服務器。此檢查之外的任何代碼均在中運行,兩者均爲

meteor method example

需要注意的是在上面的,而只有在瀏覽器中運行console.log("hi i'm client")服務器只運行console.log("hi i'm server")圖片。兩者都運行isSimulation檢查之外的代碼。

一些擔憂:

  1. 如果服務器返回的值是從客戶端返回的值不同?

    result!= mecObjects所以你需要正確處理這種情況。

  2. 如果客戶端運行失敗會怎麼樣?服務器仍在運行嗎?

    是的,服務器仍在運行。但是,您可以通過添加另一個選項throwStubExceptions: true來防止客戶端 失敗的服務器運行。這個選項將 拋出客戶端異常,您可以在try catch中捕獲這些異常。

  3. 如果客戶端和服務器mongo更新有什麼不同?

    服務器mongo更改覆蓋客戶端mongo更改。

多見於 https://guide.meteor.com/methods.htmlhttps://forums.meteor.com/t/how-to-return-value-on-meteor-call-in-client/1277/2

相關問題