So, I currently work on my first node.js project where I created an api. I would like to create one endpoint, where i would like to give 3 parameters in GET request method: "base" , "target" and "amount", and after retrieving data from mongoDb and apropriate calculations I would like the response to return the given amount given in target currency! The problem is that to access the ratios I get undefined!
my code for this endpoint is:
router.get('/convert', async(req,res)=>{
let base = req.body.base;
let target = req.body.target;
let amount = req.body.amount;
const getRatios = await Ratios.findOne({base: base});
if(!getRatios) return res.status(400).send('No such a currency found');
try{
res.send(getRatios.ratios.target);
}
catch(err){
console.log('something bad happened');
console.log(getRatios.ratios);
}
})
The only way to reach the values of the stored ratios is in console after I hard code the desired "target" i would like to access! eg console.log(getRatios.ratios.target)
returns undefined
but console.log(getRatios.ratios.AUD)
returns the rate for AUD
This only works in console, but when I try to get a response :
res.send(getRatios.ratios.target)
or res.send(getRatios.ratios.AUD)
returns nothing!
the res.send(getRatios.ratios)
works fine and returns:
{
"EUR": 0.82110323,
"AUD": 1.2923873,
"JPY": 109.49154,
"USD": 1,
"CHF": 0.89690769
}
however i would like to get access the object's values!