Javascript 如何从客户机中的Meteor.call()分配外部值?

Javascript 如何从客户机中的Meteor.call()分配外部值?,javascript,asynchronous,meteor,Javascript,Asynchronous,Meteor,我有一个类似的情况,如中所述。已定义了一个服务器Meteor方法,该方法接收调用第三方库以获取exchange结果的对象。在客户端使用Meteor.call调用时,我需要将结果值分配给外部变量。然而,我正在以我现在的方式进行未定义(我猜这是因为该方法的异步行为),如何改进下面的代码 //Method on the client side (React.JS Component) callRatesConvert(fromCurrency, toCurrency, amount) { co

我有一个类似的情况,如中所述。已定义了一个服务器Meteor方法,该方法接收调用第三方库以获取exchange结果的对象。在客户端使用Meteor.call调用时,我需要将结果值分配给外部变量。然而,我正在以我现在的方式进行未定义(我猜这是因为该方法的异步行为),如何改进下面的代码

//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;
    }

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

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

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

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

你有一个问题,不是在通话中,而是在你的代码中,我将添加一些注释

//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.
    }
因此,一个解决方案可能是使用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)
        }
    });
    }

希望能有帮助。

您有问题,不是在通话中,而是在您的代码中,我将添加一些注释

//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.
    }
因此,一个解决方案可能是使用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)
        }
    });
    }

希望能有所帮助。

非常感谢您的回复。它起作用了。但是,我避免过度使用会话集。看来回复ES6承诺也不错。非常感谢您的回复。它起作用了。但是,我避免过度使用会话集。似乎返回ES6承诺也很好。