2017-08-10 54 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

相关问题