'Why is my function returning a Promise { <pending> } instead of my entries?
Problem:
I want to return companies of a certain range (km) from a certain location. These companies are in a database that currently contains 2 entries for testing. Among other things, I also use the distance matrix API from Google to calculate the distance.
After it didn't work a debug showed me that the function returns[Promise {<pending>}, Promise {<pending>}].
Code:
const
axios = require("axios"),
knex = require('knex')(require('../knexfile'));
const getAllByDistance = (location) =>
knex('companies')
.select()
.then(entries =>
entries.map(company =>
getDistance(location, `${company.street}, ${company.postcode} ${company.place}`)
.then(distance => {
knex('companies')
.select()
.where(parseInt(company.maximum_distance_km) >= parseInt(distance.toString().slice(0, -3)))
}))
);
const getDistance = async (loc1, loc2) => {
const origins = encodeURI(`?origins=${loc1}`);
const destinations = encodeURI(`&destinations=${loc2}`);
const key = `&key=${process.env.VUE_APP_GOOGLE_MAPS_API_KEY}`;
const config = {
method: 'get',
url: `https://maps.googleapis.com/maps/api/distancematrix/json${origins}${destinations}${key}`,
headers: {}
};
return await axios(config)
.then((response) => {
return response.data['rows'][0]['elements'][0]['distance'].value;
})
.catch((err) => {
console.log(err);
});
}
The function call with debug:
companyService
.getByDistance(location)
.then(companies => {
console.log(companies)
res.status(200);
res.json(companies);
})
.catch(err => {
res.status(500);
res.end(`Error: ${err.message}`);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
