Reputation: 671
I'm trying to get a document inside by using the firebase-admin within a trigger cloud function in Firebase :
exports.onTransactionCreated = functions.firestore
.document("transaction/{id}")
.onCreate((snapshot, context) => {
const first = Date.now();
admin.firestore().collection('myCollection').doc(snapshot.data().myDocumentId).get()
.then((documentSnapshot) => {
const second = Date.now();
functions.logger.log(`seconds total = ${Math.floor((third - first) / 1000)}`);
}
}
The console log show this result :
seconds bw 1-2 elapsed = 140
Versions used :
"engines": {
"node": "12"
},
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"
}
On what circumstances a document could be that long to be retrieved ? Even in the case of cold starting, i can't believe it would be that long. This problem is actually a big pain point for my application and any help would be greatly appreciated.
Upvotes: 0
Views: 129
Reputation: 317467
Your function needs to return a promise that resolves after all the async work is complete. Right now, your function returns nothing, which means that it will terminate without waiting for anything.
Minimally, you should return the promise returned by get().then(...)
.
exports.onTransactionCreated = functions.firestore
.document("transaction/{id}")
.onCreate((snapshot, context) => {
const first = Date.now();
return admin.firestore().collection('myCollection').doc(snapshot.data().myDocumentId).get()
.then((documentSnapshot) => {
const second = Date.now();
functions.logger.log(`seconds total = ${Math.floor((third - first) / 1000)}`);
}
}
Please see the documentation for more information. Proper handling of promises is critical to making functions work correctly.
Upvotes: 1