JS 函数返回未定义
JS Function returning undefined
我做了一个函数,我希望它根据某些条件 return 是真还是假:
var functions = {
BalanceTopUp: function(value, user, referenceCode, date, madeby){
var newInvoice = {
type: "BalanceTopUp",
value: value,
balance_before: user.balance,
balance_after: (user.balance*100000 + value*100000)/100000,
date: date,
forUser: user.code,
referenceCode: referenceCode,
made_by_user: madeby.code
};
Invoice.find({referenceCode: referenceCode}, function(erR, invoices){
if(!erR){
if(invoices.length === 0){
Invoice.create(newInvoice, function(error1, created){
if(!error1){
MoneyInvoice.find({referenceCode: referenceCode}, function(err, found){
if(found[0].status == 2){
return true
}else{
return false
}
})
}else{
console.log(error1);
return false
}
});
}else{
return false
}
}else{
console.log(erR)
return false
}
})
}
当我尝试这样调用该函数时:
console.log(await functions.BalanceTopUp(100, student[0], 5200225958146, moment().format("DD/MM/YYYY - h:mm:ss a"), student[0]))
return未定义,我认为问题出在作用域上,但我无法解决,在此先感谢
你在 BalanceTopUp
函数中没有 return
,这就是你得到 undefined
的原因。 await
是没有必要的。
我做了一个函数,我希望它根据某些条件 return 是真还是假:
var functions = {
BalanceTopUp: function(value, user, referenceCode, date, madeby){
var newInvoice = {
type: "BalanceTopUp",
value: value,
balance_before: user.balance,
balance_after: (user.balance*100000 + value*100000)/100000,
date: date,
forUser: user.code,
referenceCode: referenceCode,
made_by_user: madeby.code
};
Invoice.find({referenceCode: referenceCode}, function(erR, invoices){
if(!erR){
if(invoices.length === 0){
Invoice.create(newInvoice, function(error1, created){
if(!error1){
MoneyInvoice.find({referenceCode: referenceCode}, function(err, found){
if(found[0].status == 2){
return true
}else{
return false
}
})
}else{
console.log(error1);
return false
}
});
}else{
return false
}
}else{
console.log(erR)
return false
}
})
}
当我尝试这样调用该函数时:
console.log(await functions.BalanceTopUp(100, student[0], 5200225958146, moment().format("DD/MM/YYYY - h:mm:ss a"), student[0]))
return未定义,我认为问题出在作用域上,但我无法解决,在此先感谢
你在 BalanceTopUp
函数中没有 return
,这就是你得到 undefined
的原因。 await
是没有必要的。