Skip to content

Instantly share code, notes, and snippets.

@lyleaf
Created September 30, 2019 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyleaf/4a8edcf7dcb4dd9daf6fa174b95145a2 to your computer and use it in GitHub Desktop.
Save lyleaf/4a8edcf7dcb4dd9daf6fa174b95145a2 to your computer and use it in GitHub Desktop.
Loop an array asynchronously with map
exports.getTranslations = functions.https.onRequest(async (req, res) => {
const english_words = req.body.english_words || [];
console.log(english_words);
const collectionRef = admin.firestore().collection("translations");
const createResponse = (res) => {
var data = {
english_word: (res === undefined) ? '' : res.english_word ,
translation: (res === undefined) ? '' : res.translation ,
transliteration: (res === undefined) ? '' : res.transliteration,
sound_link: (res === undefined) ? '' : res.sound_link
}
return data;
}
const promises = english_words.map(async english_word => {
return collectionRef.doc(english_word).get()
})
Promise.all(promises).then(docs => {
translations = docs.map(x => createResponse(x.data()))
console.log(translations)
res.status(200).send(translations)
}).catch(function(error) {
console.log("Internal server error", error);
res.status(500).send(error)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment