I am trying to use a couple of Google Cloud Functions to synchronize data in between Firebase and Firestore.
I made a Cloud function for updating Firestore with new data from Firebase, triggering on create in Firestore. This works perfectly fine!
However when I try to do this the other way around, using
admin.database().ref('etc etc..').get().then(...)
instead of
admin.firestore().doc('etc etc..').get().then(...)
I get an error log saying this when invoking the cloud function:
TypeError: admin.database(...).ref(...).get is not a function at exports.UpdateFirebase.functions.firestore.document.onCreate (/user_code/index.js:27:68) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:728:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Here is the issued code I have deployed as a Google Cloud Function:
exports.UpdateFirebase = functions.firestore.document('/commands/{pushId}')
.onCreate((snapshot, context) => {
admin.database().ref('/commands/' + context.params.pushId).get().then(doc => {
if (!doc.exists) {
return admin.database().ref('/commands/' + context.params.pushId).set(snapshot);
}
else return null;
}).catch((fail) => {
console.log(fail.message);
})
});
This is the working example, that does exactly the same, the other way around:
exports.UpdateFirestore = functions.database.ref('/commands/{pushId}')
.onCreate((snapshot, context) => {
admin.firestore().doc('/commands/' + context.params.pushId).get().then(doc => {
if (!doc.exists) {
return admin.firestore().doc('/commands/' + context.params.pushId).set(snapshot.val());
}
else return null;
}).catch((fail) => {
console.log(fail.message);
})
});
I have been searching for a while no answers, but to use admin.database() when wanting to retrieve stuff or write stuff to firebase from a Google Cloud Function.
If anyone have any suggestions to this error, please do comment!