I'm using a Cloud Function that performs the following steps:
- fetch some data from an API
- prepare the data previously retrieved
- store the data in Firestore
This is the code I'm using:
exports.syncItems = functions.https.onRequest((request, response) => {
sync('url', 'colName').then(() => {
response.status(200).send('Success!');
}).catch((error) => {
response.status(404).send('Failure!');
});
});
async function sync(url, colName) {
const response = await fetchData(url); // async function
const items = prepareData(response); // not async function
return await importData(colName, items); // async function
}
async function importData(colName, items) {
const colRef = firestore.collection(colName);
const batch = firestore.batch();
items.forEach(item => {
let docId = item.identifier;
let docRef = colRef.doc(`${docId}`);
batch.set(docRef, {
// set data here
});
});
return await batch.commit();
}
Behind the hood accessing Firestore is mediated by AdminSDK.
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
projectId: 'myProjectId'
});
Data import happens very fast when using Firebase emulators. Firestore shows the collection and the associated documents almost instantly.
Instead, when deploying syncItems
Google Cloud Function to Firebase I see a delay (even 2/3 minutes).
Is there any possible reason for that?