我正在调用一个执行Stripe API请求的函数,但是当我返回bool时没有任何反应。该函数被调用但if语句中没有任何内容被调用,而是调用else语句。
if(performStripeAction() == true) {
console.log("action performed"); //does not get printed
} else {
console.log("error transferring"); //prints error transferring
}
function performStripeAction() {
var psaret;
console.log("about to start transfer");
stripe.transfers.create(
{
amount: 10000,
currency: 'usd',
destination: act_xxxxx
}, function(err, transfer){
if(!err){
console.log("no error"); //prints no error
psaret = true;
}else{
console.log("error");
psaret = false;
}
console.log("the bool is " + psaret); //prints true
return psaret;
});
}
答案 0 :(得分:0)
stripe.transfers.create({
amount: 10000,
currency: 'usd',
destination: act_xxxxx },{stripe_account: String(accountId)},
function(error, transfer) {
if (error) {
// Transfer failed
return console.log("error transferring"); //prints error transferring
}
// Transfer succeeded.You can then use "transfer" object to cal another function
console.log("action performed");
});
答案 1 :(得分:0)
只需使用Stripe API调用return a promise:
performStripeAction()
.then(function(ret) {
if (ret)
console.log('action performed');
else
console.log('error transferring')
});
function performStripeAction() {
var psaret;
console.log("about to start transfer");
return stripe.transfers.create(
{
amount: 10000,
currency: 'usd',
destination: act_xxxxx
}
)
.then(function(transfer){
console.log("no error"); //prints no error
psaret = true;
return psaret;
})
.catch(function(err)){
console.log("error");
psaret = false;
return psaret;
});
}