Reputation: 385
I am trying to fetch something through a loop and than return the value after await. However it just returns a array of promises even though I am awaiting?
My goal is to get values from the https://frost.met.no/sources/v0.jsonld?types=SensorSystem&elements=${element}&country=NO&fields=id%2Cvalidfrom with the different element and it works the problem is just that I cant seem to return the real value is something is wrong with the way I Await it.
const fetchDetails=["mean(air_temperature P1M)","max(air_temperature P1M)","min(air_temperature P1M)"]
let status = await fetchData(fetchDetails);
//this returns promise but I want to await for the results.
async function fetchData(fetchDetails) {
try {
await db.query("DROP TABLE IF EXISTS weather;");
await db.query(
`CREATE TABLE weather(
weather_id SERIAL UNIQUE PRIMARY KEY NOT NULL,
source_id VARCHAR(10) NOT NULL,
valid_from VARCHAR(50),
element VARCHAR(50),
CONSTRAINT fk_source
FOREIGN KEY(source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
);`
);
const sources = await db.query("select (source_id) from sources;");
console.log(sources.rows)
const details = await fetchDetails.map(async (element) => {
await fetch(
`https://frost.met.no/sources/v0.jsonld?types=SensorSystem&elements=${element}&country=NO&fields=id%2Cvalidfrom`,
{
method: "get",
body: JSON.stringify(),
headers: {
Authorization:
"Basic YjVlNmEzODEtZmFjNS00ZDA4LWIwNjktODcwMzBlY2JkNTFjOg==",
},
}
)
.then((res) => res.json())
.then(async (data) => {
const id = "SN59450"
let sqlInsert = data.data.filter(e => e !== null && sources.rows.some(b => b.source_id === e.id)).map((value) => {
return `('${value.id}','${value.validFrom}','${element}')`
})
//fs.writeFileSync('./data6.json', JSON.stringify(sqlInsert, null, 2), 'utf-8');
let input = await db.query("INSERT INTO weather(source_id, valid_from, element) values" + sqlInsert+"returning *");
return input.rows;
});
})
//Details here is also a promise how can I wait for the loop to finish
return(details)
}
catch (err) {
console.log(err)
}
}
Upvotes: 0
Views: 41
Reputation: 1232
Use Promise.all(). Like this
async function fetchData(fetchDetails) {
try {
await db.query("DROP TABLE IF EXISTS weather;");
await db.query(
`CREATE TABLE weather(
weather_id SERIAL UNIQUE PRIMARY KEY NOT NULL,
source_id VARCHAR(10) NOT NULL,
valid_from VARCHAR(50),
element VARCHAR(50),
CONSTRAINT fk_source
FOREIGN KEY(source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
);`
);
const sources = await db.query("select (source_id) from sources;");
console.log(sources.rows)
const details = await Promise.all(fetchDetails.map(async (element) => {
await fetch(
`https://frost.met.no/sources/v0.jsonld?types=SensorSystem&elements=${element}&country=NO&fields=id%2Cvalidfrom`,
{
method: "get",
body: JSON.stringify(),
headers: {
Authorization:
"Basic YjVlNmEzODEtZmFjNS00ZDA4LWIwNjktODcwMzBlY2JkNTFjOg==",
},
}
)
.then((res) => res.json())
.then(async (data) => {
const id = "SN59450"
let sqlInsert = data.data.filter(e => e !== null && sources.rows.some(b => b.source_id === e.id)).map((value) => {
return `('${value.id}','${value.validFrom}','${element}')`
})
//fs.writeFileSync('./data6.json', JSON.stringify(sqlInsert, null, 2), 'utf-8');
let input = await db.query("INSERT INTO weather(source_id, valid_from, element) values" + sqlInsert+"returning *");
return input.rows;
});
}))
//Details here is also a promise how can I wait for the loop to finish
return(details)
}
catch (err) {
console.log(err)
}
}
Upvotes: 3