I've created a node.js function that finds documents in my firestore database, and it works successfully, but I'm querying it with .get()
Is there any way I can trigger a function that will await to complete until a document is found (as the documents are uploaded by a different app) and set on a timer, so that if the timer runs out and no documents are found (say 5 minutes) it will return a null document value?
My current function looks like this right now:
export const createFinder = async (latitude, longitude, orderUid) => {
// Find cities within 5km of location
const radiusInM = 50 * 100;
const bounds = geofire.geohashQueryBounds([latitude, longitude], radiusInM);
const promises = [];
for (const b of bounds) {
const q = db.collection('availableDocuments')
.orderBy('geohash')
.startAt(b[0])
.endAt(b[1]);
const snapshot = await q.get();
promises.push(snapshot);
}
return promises;
}
CodePudding user response:
It seems your are looking for Firestore triggered Cloud Functions. You can use onCreate()
that'll trigger a function every time a document is added.
exports.fnName = functions.firestore
.document('col/{docId}')
.onCreate((snap, context) => {
console.log(snap.data())
});
I don't see a Cloud Function in your question. Are you running this logic somewhere else? You can use a Firestore listener then as shown below:
const q = "..." // your query
q.onSnapshot((qSnap) => {
snapshot.docChanges().forEach((change) => {
if (change.type === "added") {
console.log("New Doc: ", change.doc.data());
// Run function
}
});
})