【问题标题】:I can not read the data on Firebase Functions我无法读取 Firebase Functions 上的数据
【发布时间】:2019-09-01 13:07:57
【问题描述】:

我将 firebase 函数用于推送通知触发器。

我可以发送通知,但在触发器启动时我无法读取某些数据。

我尝试了常用的 firebase 数据库功能,但它们不起作用。

const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

let fcm = ''
exports.sendNewMessageNotification = functions.database.ref('/xxx/userInformations/{uid}/')
    .onWrite((change:any) => {
    // console.log(change.before)
      const payload = {
          notification: {
              title: 'Kahve Falı',
              body: 'Falın geldi!',
      }
    }
    admin.database().ref('/xxx/userInformations/{uid}/fortunes')
    .once('value')
    .then((snapshot:any) => {
      console.log('snapshot.val()')//I want to get that snaphot.val()

      });
      fcm = change.before._data.fcm;
      return admin.messaging().sendToDevice(fcm, payload);
    });

它不会读取该快照。它以不同的方式返回 null 或 undefined。 我不能那样做这条线; .then((snapshot) => {

如果我不输入:any,它会给我一个 VS Code 错误。

【问题讨论】:

    标签: node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您似乎弄错了代码的两个部分。


    首先你有:

    exports.sendNewMessageNotification = functions.database.ref('/xxx/userInformations/${uid}/')
    

    这声明了一个云函数。当您使用 Firebase 部署此代码时,它会识别此 Cloud Function 并将字符串文字 '/xxx/userInformations/${uid}/' 解释为此代码需要触发的路径。它将路径中/xxx/userInformations 之后的值放入名为uid 的参数中。


    接下来是:

    admin.database().ref('/xxx/userInformations/${uid}/fortunes')
    

    这是常规的 JavaScript 代码,在这种情况下,'/xxx/userInformations/${uid}/fortunes' 只是一个发送到 Firebase 客户端的字符串。由于路径中不允许使用 $,因此 Firebase 客户端会引发错误。

    您可能希望路径包含uid 参数的,您可以这样做:

    admin.database().ref(`/xxx/userInformations/${context.params.uid}/fortunes`)
    

    所以变化:

    1. 使用反引号标记字符串,以便将其解释为ES6 Template Literal
    2. 在其中使用${context.params.uid},获取用于触发代码的uid 的值。

    这要求您在 Cloud Function 声明中定义 context,例如:

    .onWrite((change:any, context: any) => {
    

    【讨论】:

    • 你是个好人!非常感谢你!我希望你能过上美好而幸福的生活。非常感谢你!您的答案非常清楚,不仅是代码,还有知识。无论如何,我喜欢 Firebase,仅供参考!
    • 不客气。 :) 不要忘记也支持@AbdulhaqShaq 的回答,因为他们最初指出了模板字符串文字问题。
    【解决方案2】:

    问题在于模板文字${expression}。您没有正确提供 uid

    如果您使用的是 ES6,请使用模板文字,或者您可以使用 +,例如将 uidurl 连接。

    const admin = require('firebase-admin')
    admin.initializeApp(functions.config().firebase)
    
    let fcm = ''
    exports.sendNewMessageNotification = functions.database.ref('/xxx/userInformations/${uid}/')
        .onWrite((change:any) => {
        // console.log(change.before)
          const payload = {
              notification: {
                  title: 'Kahve Falı',
                  body: 'Falın geldi!',
          }
        }
        admin.database().ref(`/xxx/userInformations/${uid}/fortunes`)
        .once('value')
        .then((snapshot:any) => {
          console.log('snapshot.val()')//I want to get that snaphot.val()
    
          });
          fcm = change.before._data.fcm;
          return admin.messaging().sendToDevice(fcm, payload);
        });
    

    【讨论】:

    • 我得到那个错误; "/xxx/userInformations/${uid}/fortunes". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
    • 这意味着您没有使用反引号,并且 ${uid} 按原样发送到数据库(这使其成为无效路径)。
    • 当我使用反引号时,我无法部署 t。我收到此错误src/index.ts:15:58 - error TS2304: Cannot find name 'uid'. 15 admin.database().ref(/xxx/userInformations/${uid}/fortunes) @Frank van Puffelen
    • 如果提供错误,为什么这会起作用? functions.database.ref('/xxx/userInformations/{uid}/')
    • 在您部署代码时,Firebase 会解析声明您的 Cloud Function 的行,Firebase 会读取文字字符串并确定要触发的内容。带有admin.database() 的函数inside 行只是普通的JavaScript 代码。要让 JavaScript 用它的值替换 ${uid},你必须把它放在反引号中。
    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多