Firebase Cloud Function with Firestore returning “Deadline Exceeded”

Clash Royale CLAN TAG#URR8PPP
Firebase Cloud Function with Firestore returning “Deadline Exceeded”
I took one of the sample functions from the Firestore documentation and was able to successfully run it from my local firebase environment. However, once I deployed to my firebase server, the function completes, but no entries are made in the firestore database. The firebase function logs show "Deadline Exceeded." I'm a bit baffled. Anyone know why this is happening and how to resolve this?
Here is the sample function:
exports.testingFunction = functions.https.onRequest((request, response) =>
var data =
name: 'Los Angeles',
state: 'CA',
country: 'USA'
;
// Add a new document in collection "cities" with ID 'DC'
var db = admin.firestore();
var setDoc = db.collection('cities').doc('LA').set(data);
response.status(200).send();
);
doc(...).set(data)
return db.collection('cities').doc('LA').set(data).then(result => response.status(200))
@Ramon changing it to a promise did remove the error from the logs, but unfortunately did not successfully insert the data into the collection.
– Scott D
Oct 9 '17 at 21:57
2 Answers
2
Firestore has limits.
Probably “Deadline Exceeded” happens because of its limits.
See this. https://firebase.google.com/docs/firestore/quotas
Maximum write rate to a document 1 per second
https://groups.google.com/forum/#!msg/google-cloud-firestore-discuss/tGaZpTWQ7tQ/NdaDGRAzBgAJ
any workaround suggestion on this? I want to do an intial data import of 1500000 data while getting it processed using node. Any suggestion appreciated.
– KD.
Jan 19 at 16:08
@KD. 1 per second limit is for to a document. For database, the limit is "Maximum writes per second per database | 2,500 (up to 2.5 MiB per second)". you can avoid this limits using setTimeout though it takes time.
– Nobuhito Kurose
Jan 21 at 6:21
Thanks. Adding timeout would be my last option to try as I have too much data for initial input. I really wish there was a way to import JSON just like real-time db. For now I am going ahead with real-time db since the same approach works fine with it.
– KD.
Jan 21 at 6:24
I have "solved" it with 2 steps. 1) I use batch writes. And set the batch to 490 (max is apparently 500) 2) I wait that each batch finished, before I sent the next one.
– Jürgen Brandstetter
Feb 22 at 16:07
I have written this little script which uses batch writes (max 500) and only write one batch after the other.
use it by first creating a batchWorker let batch: any = new FbBatchWorker(db);
Then add anything to the worker batch.set(ref.doc(docId), MyObject);. And finish it via batch.commit().
The api is the same as for the normal Firestore Batch (https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes) However, currently it only supports set.
let batch: any = new FbBatchWorker(db);
batch.set(ref.doc(docId), MyObject);
batch.commit()
set
import firestore from "firebase-admin";
export default class FbBatchWorker
db: firestore.Firestore;
batchList: FirebaseFirestore.WriteBatch = ;
elemCount: number = 0;
constructor(db: firestore.Firestore)
this.db = db;
this.batchList.push(this.db.batch());
async commit(): Promise<any>
let batchProms: Promise<any> = ;
for (let _batch of this.batchList)
(await _batch.commit());
console.log("finished writing batch");
// batchProms.push(_batch.commit());
// return Promise.all(batchProms);
return Promise.resolve("yeah");
set(dbref: FirebaseFirestore.DocumentReference, data: any): void
this.elemCount = this.elemCount + 1;
if (this.elemCount % 490 === 0)
this.batchList.push(this.db.batch());
this.batchList[this.batchList.length - 1].set(dbref, data);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not sure if it's related to the error you're seeing but you probably want to wait for the promise returned by
doc(...).set(data)to resolve, by usingreturn db.collection('cities').doc('LA').set(data).then(result => response.status(200))– Ramon
Oct 9 '17 at 21:51