I add data to Firestore like this:
db
.collection('foo')
.add({foo: 'bar'})
.then(docRef => {
console.log('Added Foo: ', docRef.id)
// do some stuff here with the newly created foo and it's id.
})
.catch(console.error)
After the document creation, I would like to work with the new doc or specially it's ID. The document is stored in the local database with a valid ID.
But how do I get the ID after the document creation? The promise will not be resolved until the data has synced with the server.
You can get Id even before you are saving locally. You just use this way to write data.
For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method
To check whether you're receiving data from the server or the cache, use the fromCache property on the SnapshotMetadata in your snapshot event. IffromCache is true, the data came from the cache and might be stale or incomplete. If fromCache is false, the data is complete and current with the latest updates on the server.
By default, no event is raised if only the SnapshotMetadata changed. If you rely on the fromCache values, specify the includeMetadataChanges listen option when you attach your listen handler.
So if you add new data and you have offline enabled your data will be added to cache and can be listened by the listeners.
});
I came across this workaround that does the job for me.
It works, but I don't really like it because of:
change.doc._document.hasLocalMutations
to check if it is a newly created item