0
votes

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!

2

2 Answers

0
votes

Please read the documentation on reading and writing Realtime Database. There is no get() method on Reference like there is on DocumentReference. To read data from Realtime Database, you use the once() method, and pass it "value" to get a promise that resolves with a snapshot of data.

0
votes

I found the issue.

I had to use reference (ref) and child, instead of using get() and then(...)

Here is the working Cloud Function Code, triggering onCreate in Firestore, creating JSONObject in Firebase, if no object with the given document id from firestore snapshot exists:

exports.UpdateFirebase = functions.firestore.document('/commands/{pushId}')
.onCreate((snapshot, context) => {
    const ref = admin.database().ref('/commands/');
    if (!ref.child(context.params.pushId).exists) {
        return ref.child(context.params.pushId).set(snapshot.data());
    }
    else return null;
});