Created
September 28, 2019 21:39
-
-
Save Bon2xl/e2755dc9cb5a4ba28d2243fed8143d2b to your computer and use it in GitHub Desktop.
Firestore Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//********************************************** | |
// 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