简体   繁体   English

从Firebase提取数据-Google云功能

[英]Fetching data from Firebase - Google Cloud Functions

I am trying to use a couple of Google Cloud Functions to synchronize data in between Firebase and Firestore. 我正在尝试使用几个Google Cloud Functions在Firebase和Firestore之间同步数据。

I made a Cloud function for updating Firestore with new data from Firebase, triggering on create in Firestore. 我做了一个Cloud函数,用于使用Firebase中的新数据更新Firestore,并在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. TypeError:admin.database(...)。ref(...)。get在exports.UpdateFirebase.functions.firestore.document.onCreate(/user_code/index.js:27:68)处不是函数。 (/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) (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)在(native)在__awaiter的/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)位于/ var /处的cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)在process._tickDomainCallback处的tmp / worker / worker.js:728:24(内部/process/next_tick.js:135:7)

Here is the issued code I have deployed as a Google Cloud Function: 这是我已部署为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. 我一直在搜索没有答案,但是想要从Google Cloud Function检索内容或将内容写入firebase时使用admin.database()。

If anyone have any suggestions to this error, please do comment! 如果有人对此错误有任何建议,请发表评论!

Please read the documentation on reading and writing Realtime Database . 请阅读有关读写实时数据库的文档。 There is no get() method on Reference like there is on DocumentReference. 像DocumentReference一样, Reference上没有get()方法。 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. 要从实时数据库中读取数据,请使用Once()方法,并将其"value"传递给它,以使用数据快照来解析承诺。

I found the issue. 我发现了问题。

I had to use reference (ref) and child, instead of using get() and then(...) 我必须使用引用(ref)和子元素,而不是使用get()然后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: 如果不存在来自Firestore快照的具有给定文档ID的对象,这是有效的Cloud Function代码,在Firestore中触发onCreate,在Firebase中创建JSONObject:

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google云功能从Firebase实时数据库中读取值? - How to read a value from Firebase realtime database with a Google cloud functions? 使用JavaScript Promise从Firebase云功能读取Google表格 - Reading Google Sheets from Firebase cloud functions using javascript promises 使用 firebase 函数重定向到 URL - 谷歌云函数 - Redirecting to URL with firebase functions - google cloud functions 如何从Firebase Cloud功能删除用户数据(任何数据)? - How to delete user data (any data) from Firebase Cloud functions? 从 Firestore Cloud Functions nodejs 获取时间戳 - Fetching timestamp from firestore Cloud Functions nodejs 如何使用Cloud函数从Firebase查询特定数据 - How to query specific data from Firebase using Cloud functions 使用 Firebase Cloud Functions 从外部 API 获取数据(超时) - Using Firebase Cloud Functions to fetch data from an external API (Timeout) 从Firebase Cloud Functions将数据发送回Android - Sending data back to Android from Firebase Cloud Functions 通过云功能将数据从Firebase数据库写入文件? - Write data from firebase-database to a file through cloud functions? Firebase的Cloud Functions:如何从数据库同步读取数据? - Cloud Functions for Firebase: how to read data from Database synchronously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM