Using the web/JS platform, I want to retrieve the latest document added to a collection.
- How do I accomplish this?
- Do I need to append a timestamp when I save the data?
- Does the server automatically append a timestamp in the background on
doc.add()?
db
.collection("cities")
// .orderBy('added_at', 'desc') // fails
// .orderBy('created_at', 'desc') // fails
.limit(1)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(doc.id, " => ", doc.data());
// console.log('timestamp: ', doc.timestamp()); // throws error (not a function)
// console.log('timestamp: ', doc.get('created_at')); // undefined
});
});