Skip to content

Instantly share code, notes, and snippets.

@Bon2xl
Created September 28, 2019 21:39
Show Gist options
  • Save Bon2xl/e2755dc9cb5a4ba28d2243fed8143d2b to your computer and use it in GitHub Desktop.
Save Bon2xl/e2755dc9cb5a4ba28d2243fed8143d2b to your computer and use it in GitHub Desktop.
Firestore Query
//**********************************************
// Get all documents
//**********************************************
router.get('/', async function(req, res, next) {
const title = 'Boozeyum';
let cityRef = await db.collection('cities');
const data = [];
let getDoc = await cityRef.get()
.then(snapshot => {
snapshot.docs.forEach(doc => {
data.push(doc.data());
});
return snapshot.docs.map(doc => doc.data());
})
.catch(err => {
console.log('Error getting document', err);
return null;
});
return res.render('index', { title: title, data: data });
});
//**********************************************
// Get document item
//**********************************************
router.get('/', async function(req, res, next) {
const title = 'Boozeyum';
let cityRef = await db.collection('cities').doc('SF');
let getDoc = await cityRef.get().then(doc => {
if (!doc.exists) return null;
console.log('Document data:', doc.data());
return doc.data();
})
.catch(err => {
console.log('Error getting document', err);
return null;
});
console.log(getDoc);
return res.render('index', { title: title, data: getDoc });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment