I am learning NodeJS currently working on my first project a subscription based website.
I need a condition which decides whether one transaction has value at its btcTx property. This is done so when user creates a new transaction, BTC transaction gets created and added to the transactions .btcTx property. After the user presses F5 it should not create a new transaction but rather use the one already created. I have function that requires one argument a value of ID.
function tranHasBTC(tranId) {
Transaction.findById(tranId,function(err,foundTr){
if(err){
console.log(err);
} else {
return (foundTr.btcTx != undefined);
}
});
}
This function is supposed to look in mongoDB database and look if transaction passed within argument has value at btcTx.
Then here I have the function which should rely on the upper functions return value
router.get("/btc/:txId", function (req, res) {
var tranId = req.params.txId;
var tranTrue = tranHasBTC(tranId);
if (!tranTrue) {
//add new transaction
} else {
//find transaction in database and load it from there
}
});
The problem is that whenever the
var tranTrue = tranHasBTC(tranId);
gets called, it return undefined
I am assuming that I need to use some kind of async function to wait for the method to return either true or false, I have tried it with promises but I couldn't get it done, that is why I am posting here.