【问题标题】:How to Access Firebase Database Inside a Cloud Function?如何在云函数中访问 Firebase 数据库?
【发布时间】:2018-08-06 07:16:00
【问题描述】:

在下面的代码中:

exports.sendRequestNotification = functions.database.ref('/Notifications/{notificationid}/longitude').onWrite((event) => {
    const lat = admin.database().ref('/Users/B8r1Xc8cAOVGOiptlFOz45fzxSm1');
    return Promise.all([lat]).then((results) =>{
        const latitud = results[0];
        console.log('hi   ',latitud.data.child('name').val());
    });
});

我正在尝试获取特定用户的名称并将其注销,但它给了我这个错误:

TypeError: Cannot read property 'child' of undefined
at Promise.all.then (/user_code/index.js:60:35)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我在这篇文章中尝试了解决方案,但也没有用 (Access db data in Cloud Functions for Firebase)

有人可以帮帮我吗?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions firebase-admin


    【解决方案1】:

    admin.database().ref() 不返回承诺。它只返回一个Reference 对象。如果要查询引用位置的数据,请使用其once() 方法。 once() 返回一个在数据可用时解决的承诺:

    admin.database().ref('/Users/B8r1Xc8cAOVGOiptlFOz45fzxSm1').once('value')
    .then(snapshot => {
        // use the object returned here to get the data at the location of the ref
        snapshot.val()
    })
    

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 2018-05-10
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多