'JS Function returning undefined [duplicate]
I made a function and i wanted it to return true or false depending on some conditions:
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
}
})
}
when i try to call that function like that:
console.log(await functions.BalanceTopUp(100, student[0], 5200225958146, moment().format("DD/MM/YYYY - h:mm:ss a"), student[0]))
it returns undefined, i think the problem is for scopes but i couldnt solve it, thanks in advance
Solution 1:[1]
You have no return in the BalanceTopUp function, that is why you get undefined. And await is not necessary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robert Cojocaru |
