简体   繁体   English

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

[英]How to return a result from Meteor.call method in client?

I'm calling a server method from my client side code via the Meteor.call() function. 我通过Meteor.call()函数从客户端代码调用服务器方法。

But I get an undefined value when I try to pass the callback function's return value to a var within the onCreated function. 但是当我尝试将回调函数的返回值传递给onCreated函数中的var 时,我得到一个未定义的值

Looking at this solution the result is still only available within the scope of the callback: 查看此解决方案,结果仍然只在回调范围内可用:

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

Also when I read the docs for Meteor-call it explains that the method is async which explains that the return value of the function can't be assigned to a variable: 此外,当我阅读Meteor-call的文档时,它解释了该方法是异步的,这解释了函数的返回值不能分配给变量:

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

Question: 题:

How can you return a result from Meteor.call method in client? 如何从客户端返回Meteor.call方法的结果?

Code snippet of client side code: 客户端代码的代码片段:

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

});

The value of your mecObjects will always be undefined , because Meteor.call function doesn't return anything. mecObjects的值将始终undefined ,因为Meteor.call函数不返回任何内容。 Server's method call response (or error) will be passed to callback only, like in your code, actually: those error and result variables. 服务器的方法调用响应(或错误)将仅传递给回调,就像在代码中一样,实际上:那些errorresult变量。

Because the return statement is in the callback and is unblock you can't return a method value like that. 因为return语句在回调中并且是unblock,所以不能返回类似的方法值。 you can put it https://github.com/stubailo/meteor-reactive-method which will return the value but it must live inside a helper or reactive environment. 你可以把它放到https://github.com/stubailo/meteor-reactive-method这将返回值,但它必须住在帮助器或被动环境中。

short answer: 简答:

define your Meteor.Method inside common code (code available to server and client) then use Meteor.apply with the option { returnStubValue : true } 定义通用代码(可用于服务器和客户端的代码)内您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

longer answer: 更长的答案:

Meteor allows for Optimistic-UI which is simulating a server method on the browser. Meteor允许使用Optimistic-UI来模拟浏览器上的服务器方法。 To enable this, define your Meteor.Method in common code (server + client space). 要启用此功能,请在公共代码(服务器+客户端空间)中定义Meteor.Method。 This is useful because the user can see the UI updates faster since we don't need to make a server-round trip. 这很有用,因为用户可以更快地看到UI更新,因为我们不需要进行服务器往返。 流星方法流程

Notice that in the picture above, the method call runs first on the client and later on the server. 请注意,在上图中,方法调用首先在客户端上运行,然后在服务器上运行。 { returnStubValue: true } allows the caller to receive the method's return value from the client run { returnStubValue: true }允许调用者客户端运行中接收方法的返回值

When writing a Meteor method, you can use this.isSimulation to specify what logic runs exclusively on client or server. 编写Meteor方法时,可以使用this.isSimulation指定在客户端或服务器上独占运行的逻辑。 Any code outside of this check runs in both . 此检查之外的任何代码两者中运行。

流星方法的例子

Note that in the picture above that the server only runs console.log("hi i'm server") while only the browser runs console.log("hi i'm client") . 请注意,在上图中,服务器只运行console.log("hi i'm server")而只有浏览器运行console.log("hi i'm client") Both run the code outside of the isSimulation check. 两者都在isSimulation检查之外运行代码。

A few concerns: 一些问题:

  1. What if the server return value is different from the client return value? 如果服务器返回值与客户端返回值不同,该怎么办?

    result != mecObjects so you will need to handle that situation properly. 结果!= mecObjects所以你需要正确处理这种情况。

  2. What if the client run fails? 如果客户端运行失败怎么办? Does the server still run? 服务器还在运行吗?

    Yes, the server still runs. 是的,服务器仍在运行。 However you can prevent server runs on client failures by adding another option, throwStubExceptions: true . 但是,您可以通过添加另一个选项throwStubExceptions: true来阻止服务器在客户端故障上运行。 This option will throw client exceptions which you can capture in a try catch. 此选项将抛出您可以在try catch中捕获的客户端异常。

  3. What if the client and server mongo updates are different? 如果客户端和服务器mongo更新不同怎么办?

    The server mongo changes override the client mongo changes. 服务器mongo更改覆盖客户端mongo更改。

see more at https://guide.meteor.com/methods.html and https://forums.meteor.com/t/how-to-return-value-on-meteor-call-in-client/1277/2 有关详细信息,请访问https://guide.meteor.com/methods.htmlhttps://forums.meteor.com/t/how-to-return-value-on-meteor-call-in-client/1277/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM