The function findTransactionByBill
find the results but it returns as undefined
when I call it from inside the app.post
.
function findTransactionByBill(billId){
Transactions.find({billId : billId},function(err, transactions){
if(err)
res.send("Error: "+err);
console.log(transactions); //Returns the results
return transactions; // <--- Doesn't send the results to the trans variable inside the app.post below!
});
}
app.post('/api/transaction', function(req, res) {
trans = findTransactionByBill(req.body._id);
console.log("Transactions: "+trans);
Transactions.create({
billId : req.body._id,
paymentDate : Date.now(),
amount : req.body.amount,
timestamp : Date.now()
}, function(err, transactions) {
if (err)
res.send(err);
res.send(transactions);
});
});
When I log the transactions
variable, it returns the result. But when I call it from inside the app.post (trans
) it display as undefined...
All I'm trying to do is: 1) Have a common function which I can use to check if that transaction already exists. 2) Use it from any place in the application.
Maybe the way I'm using the function is wrong. Thanks in advance for the help!