I have a node server up and running where I need to monitor updates in a collection and retrieve the newly added data. My approach was to utilize db.collection("posts").onSnapshot to listen for updates and fetch the latest entry by ordering it based on date.
db.collection("posts").onSnapshot(async () => {
const newPost = await db
.collection("posts")
.orderBy("date", "desc")
.limit(1)
.get()
.data();
console.log(newPost);
});
However, when using .data(), an error occurs since it's not a recognized function for retrieving the data. After some debugging attempts, I couldn't identify any keys within the object that would grant me access to the post data.
This is the current output without utilizing .data()
QuerySnapshot$1 {
_delegate:
QuerySnapshot {
_firestore:
FirebaseFirestore$1 {
_persistenceKey: '[DEFAULT]',
_settings: [FirestoreSettings],
_settingsFrozen: true,
_app: [FirebaseAppImpl],
_databaseId: [DatabaseId],
_credentials: [FirebaseCredentialsProvider],
_queue: [AsyncQueue],
_firestoreClient: [FirestoreClient] },
_userDataWriter: UserDataWriter { firestore: [Firestore] },
_snapshot:
ViewSnapshot {
query: [QueryImpl],
docs: [DocumentSet],
oldDocs: [DocumentSet],
docChanges: [Array],
mutatedKeys: [SortedSet],
fromCache: false,
syncStateChanged: true,
excludesMetadataChanges: false },
metadata:
SnapshotMetadata { hasPendingWrites: false, fromCache: false },
query:
Query {
_converter: null,
_query: [QueryImpl],
type: 'query',
firestore: [FirebaseFirestore$1] } },
_firestore:
Firestore {
_delegate:
FirebaseFirestore$1 {
_persistenceKey: '[DEFAULT]',
_settings: [FirestoreSettings],
_settingsFrozen: true,
_app: [FirebaseAppImpl],
_databaseId: [DatabaseId],
_credentials: [FirebaseCredentialsProvider],
_queue: [AsyncQueue],
_firestoreClient: [FirestoreClient] },
_persistenceProvider: IndexedDbPersistenceProvider {},
INTERNAL: { delete: [Function: delete] },
_appCompat:
FirebaseAppImpl {
firebase_: [Object],
isDeleted_: false,
name_: '[DEFAULT]',
automaticDataCollectionEnabled_: false,
options_: [Object],
container: [ComponentContainer] } } }