如何在客户端中从 Meteor.call() 分配外部值?
How to assign outer value from Meteor.call() in client?
我的情况与中所述类似。定义了一个服务器 Meteor 方法,该方法接收调用第三方库以交换结果的对象。在客户端调用 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;
}
将 callRatesConvert
转换为 returns Promise 的函数。如果需要,您还可以使用 shorthand 属性 赋值来减少代码的语法噪音:
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 的 Session
:
//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 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;
}
将 callRatesConvert
转换为 returns Promise 的函数。如果需要,您还可以使用 shorthand 属性 赋值来减少代码的语法噪音:
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 的 Session
:
//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)
}
});
}
希望对您有所帮助。