代码之家  ›  专栏  ›  技术社区  ›  Yajairo87 Streets Of Boston

如何分配流星的外部值。在客户端中调用()?

  •  0
  • Yajairo87 Streets Of Boston  · 技术社区  · 7 年前

    我的情况与 this question 。定义了一个服务器Meteor方法,该方法接收调用第三方库以获取exchange结果的对象。当用流星召唤时。在客户端调用时,我需要将结果值分配给外部变量。然而,我正在以我现在的方式进行定义(我想这是因为该方法的异步行为),如何改进以下代码?

    //Method on the client side (React.JS Component)
    callRatesConvert(fromCurrency, toCurrency, amount) {
        const call = this.props.call;
        let resCall = 0; // Outer variable
        let settings = {};
        settings.fromCurrency = fromCurrency;
        settings.toCurrency = toCurrency;
        settings.amount = amount;
        settings.accuracy = 10;
    
    //Calls Backend method API that returns res successfully
        call('rates.convert', settings, (err, res) => {
            if (err) {
               //Shows UI Error to user
            } else if (res) { //res value is fetched from backend method properly
                resCall = res; // this is not being assigning properly
            }
        });
        console.log('resCall', resCall); //this prints 'undefined'
        return resCall;
        }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   CertainPerformance    7 年前

    转动 callRatesConvert 转换为返回承诺的函数。如果需要,还可以使用速记属性分配来减少代码的语法噪音:

    callRatesConvert(fromCurrency, toCurrency, amount) {
      const call = this.props.call;
      const settings = {
        fromCurrency,
        toCurrency ,
        amount,
        accuracy: 10,
      };
      return new Promise((resolve, reject) => {
        call('rates.convert', settings, (err, res) => {
          if (err) {
            //Show Error UI to user
            reject(err);
          } else if (res) {
            resolve(res);
          }
        });
      });
    }
    

    然后用

    someInstantiation.callRatesConvert(...)
      .then((resCall) => {
        // do something with the response
      });
    
        2
  •  2
  •   Prince Hernandez    7 年前

    你有问题,不是在通话中,而是在你的代码上,我会补充一些注释。

    //Method on the client side (React.JS Component)
    callRatesConvert(fromCurrency, toCurrency, amount) {
        const call = this.props.call;
        let resCall = 0; // Outer variable
        let settings = {};
        settings.fromCurrency = fromCurrency;
        settings.toCurrency = toCurrency;
        settings.amount = amount;
        settings.accuracy = 10;
    
    //Calls Backend method API that returns res succesfully
        call('rates.convert', settings, (err, res) => {
            if (err) {
               //Show Error UI to user
            } else if (res) { //res value is fetched from backend method properly
                console.log('result from call', res) //this wont be undefined.
                resCall = res; //this is assigned but it takes some time because it is still fetching
            }
        });
        console.log('resCall', resCall); //this will print undefined because it is outside the call method, and it is not assigned yet.
        return resCall; // this obviously will be undefined.
        }
    

    因此,一种解决方案可能是使用 Session 来自meteor:

    //Method on the client side (React.JS Component)
    callRatesConvert(fromCurrency, toCurrency, amount) {
        const call = this.props.call;
        let resCall = 0; // Outer variable
        let settings = {};
        settings.fromCurrency = fromCurrency;
        settings.toCurrency = toCurrency;
        settings.amount = amount;
        settings.accuracy = 10;
    
    //Calls Backend method API that returns res succesfully
        call('rates.convert', settings, (err, res) => {
            if (err) {
               //Show Error UI to user
            } else if (res) { 
                resCall = res; 
                console.log('resCall', resCall); 
                Session.set("resCall", resCall)
            }
        });
        }
    

    希望有帮助。