【问题标题】:Read Sibling Data of a particular node using Firebase Cloud Functions使用 Firebase Cloud Functions 读取特定节点的兄弟数据
【发布时间】:2021-01-29 18:10:04
【问题描述】:

我正在使用 Firebase 实时数据库和 Firebase 云功能(在 Typescript 中)。 下面是我的数据库节点的结构:

订单节点:

orders
|- {Push ID}
    |--billing_id
    |--orders_id
    

发货节点:

shippings
|- {Push ID}
    |-- billing_id
    |-- user_id

我的函数观察“orders”节点的变化并获取“billing_id”的值。

注意:此 billing_id 与存储在 “运输”节点。此 billing_id 将始终出现在“shippings”节点中。

在下一步中,我想定位包含确切 billing_id 的 shipping 节点并获取 user_id。 以下是代码(最短版本):

export const orderEmail = functions.database.ref('/orders/{pushId}')
.onCreate((snapshot,context)=>{
    const original = snapshot.val(); //Stores values from the newly created orders node
    const billing_id = original.billing_id; // Stores the billing_id from the newly created orders node

//Need help from here.
    var db = admin.database();
    var val1:any;
    db.ref(`/shippings/{pushId}/${billing_id}`).once("value") //Next what do I need to do?
});

编辑 1:

尝试了 Neelavar 建议的解决方案。以下是我在部署该函数时遇到的警告和错误。

WARNING: A:/HN/functions/src/index.ts:79:9 - Forbidden 'var' keyword, use 'let' or 'const' instead
WARNING: A:/HN/functions/src/index.ts:79:13 - Identifier 'db' is never reassigned; use 'const' instead of 'var'.
ERROR: A:/HN/functions/src/index.ts:86:24 - Shadowed name: 'snapshot'
WARNING: A:/HN/functions/src/index.ts:149:9 - Forbidden 'var' keyword, use 'let' or 'const' instead
WARNING: A:/HN/functions/src/index.ts:149:13 - Identifier 'options' is never reassigned; use 'const' instead of 'var'.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\RIYASREE'S_PC\AppData\Roaming\npm-cache\_logs\2020-10-15T14_18_07_047Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2 

注意:如果需要任何其他信息,请告诉我。 另外,请提供详细的解决方案,因为我是 Firebase 和 Typescript 的新手。

【问题讨论】:

    标签: typescript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    在 Neelavar 的帮助和一点点调整代码之后,我能够成功获取所需的值。下面是运行代码:

    export const orderEmail = functions.database.ref('/orders/{pushId}')
    .onCreate((snapshot,context)=>{
        const original = snapshot.val(); //Stores values from the newly created orders node
    
    const billing_id = original.billing_id; // Stores the billing_id from the newly created orders node
            var db = admin.database();
            var val1:any;
           return db.ref(`/shippings`)
                  .orderbyChild('billing_id')
                  .equalTo(billing_id).once("value", snap:any => {
        const data = snap.val();
        const pushId = Object.keys(data)[0]; //Gets the Push Id
        const userId = data[pushId].user_id; // Gets the user_id
        console.log("User Id", userId)
            });
        });
    

    【讨论】:

      【解决方案2】:

      由于您已经拥有来自onCreate 触发器的billing_id,现在您需要在shippings 节点中查询已知的billing_id。

          db.ref(`/shippings`)
                .orderbyChild('billing_id')
                .equalTo(billing_id).once("value", snapshot => {
                 // Now use the snapshot to access the shippings collection document
                 // which now has both billing_id and user_id - only with matching billing_id
      
          });
      

      【讨论】:

      • 嘿!我收到错误。你能再检查一下吗?我正在用错误消息编辑我的问题..
      • 如果你启用了打字稿严格检查,那么使用.once("value", (snapshot: any) => {
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2021-05-18
      • 2018-03-24
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      相关资源
      最近更新 更多