【问题标题】:How to return a result from Meteor.call method in client?如何从客户端的 Meteor.call 方法返回结果?
【发布时间】:2018-01-19 00:39:24
【问题描述】:

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

但是当我尝试将回调函数的返回值传递给onCreated 函数中的变量时,我得到了一个未定义的值

查看这个解决方案,结果仍然只在回调范围内可用:

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

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

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

问题:

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

客户端代码的代码sn-p:

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

});

【问题讨论】:

  • 不如你尝试创建一个模板响应变量。在Meteor.call 的函数内将该变量分配为Template.instance().x.set(result)
  • 你到底想做什么?从 onCreated() 返回值对您意味着什么?即使没有方法调用,这对我来说也没有意义。

标签: javascript asynchronous meteor callback


【解决方案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 更新,因为我们不需要进行服务器往返。

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

接收方法的返回值

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

请注意,上图中服务器只运行console.log("hi i'm server"),而只有浏览器运行console.log("hi i'm client")。两者都在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

【讨论】:

    【解决方案2】:

    因为 return 语句在回调中并且是 unblock 的,所以你不能返回这样的方法值。你可以把它放在https://github.com/stubailo/meteor-reactive-method 上,它会返回值,但它必须存在于帮助程序或反应式环境中。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 2013-02-13
        • 2013-03-31
        • 1970-01-01
        相关资源
        最近更新 更多