从Typescript Firebase数据库的快照中获取子值

时间:2018-07-07 16:54:34

标签: javascript typescript firebase firebase-realtime-database

无论何时在特定节点上创建新对象,我都希望从快照中获取特定值。除了“ otherPartyToken”变量始终未定义之外,下面提到的代码都可以正常工作。 如何在下面的代码中获取registrationToken的值。这是我的Returned snap / partyBDetils

            Party B Detals { 'd0d8fde8-31e0-4b76-bc27-dea6d8a0ca99': 
               { nickName: 'kalia-Don',
                 registrationId: 'd0d8fde8-31e0-4b76-bc27-dea6d8a0ca99',
                 registrationToken: 'a23kkdn23DNXMFpsoe-ekerenkneEEX0g' } } 

这是我完整的代码。

            exports.onNewRequest = functions.database.ref('Users/Room/{pushId}')
              .onCreate((snapshot, context) => {
                const mSnapShot = snapshot.val();
                console.log('Shanpshot', mSnapShot);
                const otherPartyKey = mSnapShot.otherParty
                console.log('Other Party', otherPartyKey);
                const rootRef = admin.database().ref('Users')
                const promise_BParty = rootRef.child("RegisteredUsers")
                  .orderByChild("registrationId")
                  .equalTo(otherPartyKey)
                  .once('value', function (snap) {
                    const partyBDetails = snap.val();
                    console.log('Party B Detals', partyBDetails);
                    const otherPartyToken = partyBDetails.val().registrationToken // partyBDetails.registrationToken;  snap.val().registrationToken
                    console.log('Other Party Token', otherPartyToken)
                    const payload = {
                      notification: {
                        title: "This is titble",
                        body: "This is body"
                      },
                      data: {
                        Type: "This is type",
                        Name: "this is name",
                        PeerId: "this is id"
                      }
                    }

                    console.log('Log', payload)
                    return admin.messaging().sendToDevice(otherPartyToken, payload)
                  });
                return promise_BParty
              }
              )

获取其他otherpartyToken为null或未定义。我怎样才能解决这个问题。

0 个答案:

没有答案