GET all data
Wed Jun 23 2021 14:42:44 GMT+0000 (Coordinated Universal Time)
Saved by
@joeavargas
// GET - ALL DATA
app.get('/api/read', (req, res) =>{
(async () => {
try{
let query = db.collection('products');
let response = [];
await query.get().then(querySnapshot => {
let docs = querySnapshot.docs; // result of the query
for (let doc of docs){
const selectedItem = {
id: doc.id,
name: doc.data().name,
description: doc.data().description,
price: doc.data().price
};
response.push(selectedItem);
}
return response; // each then should return a value
})
// Successfully queried data
return res.status(200).send(response);
}
catch(error){
// Handle error
console.log(error);
return res.status(500).send(error);
}
})();
});
content_copyCOPY
Comments