I'm trying to get all documents from a firebase collection, create a list and return it on request with a Cloud Function, but I'm struggling with the asynchronous nature of JavaScript. Here's my code so far:
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const items = "";
async function buildItems() {
db.collection("reminders").get().then((QuerySnapshot) => {
QuerySnapshot.forEach((item) => {
items.concat("<li>" + item.data().name + "</li>");
console.log(items);
});
})
}
exports.view = functions.https.onRequest((req, res) => {
buildItems().then(
res.status(200).send(`<!doctype html>
<head>
<title>Reminders</title>
</head>
<body>
<ul>
${items}
</ul>
</body>
</html>`))});
EDIT: Include Promises-based code I've tried (it's wrong, I don't know how to solve it)
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
function buildItems() {
return new Promise((resolve) => {
resolve(db.collection("reminders").get())
});
}
exports.view = functions.https.onRequest((req, res) => {
buildItems(reminders => {
let items = "";
reminders.then((qs) => {
qs.forEach(items.concat("<li>" + qs.data().name + "</li>"))
}).then(resolve(items));
}).then( items =>
res.status(200).send(`
<!doctype html>
<head>
<title>Reminders</title>
</head>
<body>
<ul>
${items}
</ul>
</body>
</html>`))});
The output is always the same: absolutely nothing is displayed on the browser or to the console. I've tried variations of this code, but no success so far.
Thanks in advance!