data i get from external api is not saved in firestore database
but the record I added as last name xxxxx is successful
wait db.collection(‘coins’).add({lastName: ‘xxxxxxx’}) this works but the code below does not
exports.firestoreKaydet = functions.firestore
.document("/users/{userId}")
.onCreate(async (snap, context) => {
await db.collection("coins").add({ lastName: "xxxxxxx" });
fetch(
"https://api.coinecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=true"
)
.then((response) => response.json())
.then(async (data) => {
data.forEach(async (coin) => {
await db.collection("coins").add({
name: coin.name,
});
});
})
.catch((error) => {
console.error(error);
});
});
2
Answers
You should not use
async/await
within aforEach()
loop, see "JavaScript: async/await with forEach()" and "Using async/await with a forEach loop".You can use
Promise.all()
as follows:UPDATE: After helping you debugging, it appears that
fetch
was not installed. Install it in thefunctions
directory as followsthen put the line below in the
index.js
file: