I am trying to use google-distance package of node to calculate the distance between two cities, and once this distance is stored in a mongodb database, next to other fields from a form. The problem I find is that I don't know how to return the value of the function to store it in the database and it always returns an undefined. Anyone know where I can fail?
removalsCtrl.createRemoval = async (req, res) => {
const { name, email, origin, destination } = req.body;
let kilometers = await distance.get({ origin, destination }, function (err, data) {
if (err) return console.log(err);
return data.distanceValue;
})
const newRemoval = await new Removal({
name,
email,
origin,
destination,
kilometers
})
await newRemoval.save();
res.json({ message: 'Removal Saved' })
};