如何在客户端返回Meteor.call方法的结果?

时间:2017-08-10 16:40:01

标签: javascript asynchronous meteor callback

我通过Meteor.call()函数从客户端代码调用服务器方法。

当我尝试将回调函数的返回值传递给onCreated函数中的var时,我得到一个未定义的值。

查看此解决方案,结果仍然只能在回调范围内使用:

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

当我阅读Meteor-call的文档时,它解释了该方法是异步的,这解释了函数的返回值不能分配给变量:

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();
  },

});

2 个答案:

答案 0 :(得分:1)

mecObjects的值始终为undefined,因为Meteor.call函数不会返回任何内容。服务器的方法调用响应(或错误)将仅传递给回调,就像在代码中一样,实际上是:那些errorresult变量。

答案 1 :(得分:1)

简短回答:

在公共代码中定义您的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

更长的回答:

Meteor允许使用在浏览器上模拟服务器方法的Optimistic-UI。要启用此功能,请在公共代码(服务器+客户端空间)中定义Meteor.Method。这很有用,因为用户可以更快地看到UI更新,因为我们不需要进行服务器往返。 meteor method flow

请注意,在上图中,方法调用首先在客户端上运行,然后在服务器上运行。 { returnStubValue: true }允许来电者客户端运行

接收方法的返回值

编写Meteor方法时,可以使用this.isSimulation指定在客户端或服务器上专门运行的逻辑。此检查之外的任何代码都在两者中运行。

meteor method example

请注意,在上图中,服务器仅运行console.log("hi i'm server"),而只有浏览器运行console.log("hi i'm client")。两者都在isSimulation检查之外运行代码。

一些担忧:

  1. 如果服务器返回值与客户端返回值不同,该怎么办?

    结果!= mecObjects所以你需要正确处理这种情况。

  2. 如果客户端运行失败怎么办?服务器是否仍在运行?

    是的,服务器仍在运行。但是,您可以阻止服务器在客户端上运行 通过添加另一个选项throwStubExceptions: true来实现失败。这个选项会 抛出可以在try catch中捕获的客户端异常。

  3. 如果客户端和服务器mongo更新不同怎么办?

    服务器mongo更改覆盖客户端mongo更改。

  4. 详情请见 https://guide.meteor.com/methods.htmlhttps://forums.meteor.com/t/how-to-return-value-on-meteor-call-in-client/1277/2

相关问题