首页 > 解决方案 > Firebase 云函数 - 错误:Reference.child 失败:第一个参数是无效路径

问题描述

我正在尝试使用本教程获取推送通知:https ://www.youtube.com/watch?v=z27IroVNFLI

它显然有点老了,但 Firebase 网络应用程序没有很多好的替代品。

我的云功能运行时出现以下错误:

fcm发送

错误:Reference.child 失败:第一个参数是无效路径 =“/fcmTokens/[object Object]”。路径必须是非空字符串,并且在 validatePathString (/srv/node_modules/@firebase/database/dist/index.node) 中不能包含“.”、“#”、“$”、“[”或“]” .cjs.js:1636:15) 在 validateRootPathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1647:5) 在 Reference.child (/srv/node_modules/@firebase/database /dist/index.node.cjs.js:13688:17) 在 Database.ref (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:14862:48) 在 exports.fcmSend.functions .database.ref.onWrite (/srv/index.js:21:12) 在 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) 在 /worker/worker.js: 825:24 at process._tickDomainCallback (internal/process/next_tick.js:229:

这是云功能:

exports.fcmSend = functions.database
                           .ref('/model_outputs/real_estate')
                           .onWrite((change, context) => {

    const userId  = change.after.val();
    console.log(change);
    const payload = {
          notification: {
            title: "test",
            body: "test body",
            icon: "https://placeimg.com/250/250/people"
          }
        };
  
  
     admin.database()
          .ref(`/fcmTokens/${userId}`)
          .once('value')
          .then(token => token.val() )
          .then(userFcmToken => {
            return admin.messaging().sendToDevice(userFcmToken, payload)
          })
          .then(res => {
            console.log("Sent Successfully", res);
            return null;
          })
          .catch(err => {
            console.log(err);
          });
  
  });

s in this case: 我已经看到其他人发布了有关此错误的帖子,但是当他们使用 's 而不是admin.database().ref( /fcmTokens/${userId})`时,他们中的大多数人不得不这样做。如您所见,我正在使用刻度,所以我不确定这里出了什么问题。

这是我的数据库中数据的结构方式: 在此处输入图像描述

显然,我砍掉了 ID,但我只是想说明它直接嵌套在/fcmTokens.

标签: node.jsfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


您的 Cloud Function 似乎有几个问题:

#1

以下行生成错误,因为userId它不是字符串。

admin.database().ref(`/fcmTokens/${userId}`)

DB节点的值很可能'/model_outputs/real_estate'是一个对象,因此当您const userId = change.after.val();将对象分配给userId

在您上面的评论之后更新:看来您得到undefineduserId. 您需要调试并解决这个问题:它必须是一个字符串。

#2

以下承诺链是错误的:

admin.database()
      .ref(`/fcmTokens/${userId}`)
      .once('value')
      .then(token => token.val() )  // You don't return anything here, and not a Promise
      .then(userFcmToken => {
        return admin.messaging().sendToDevice(userFcmToken, payload)
      })
      .then(res => {
        console.log("Sent Successfully", res);
        return null;
      })
      .catch(err => {
        console.log(err);
      });

如果我正确理解您的逻辑和数据模型,它应该是以下几行:

admin.database()
      .ref(`/fcmTokens/${userId}`)
      .once('value')
      .then(snapshot => { 
        const userFcmToken = snapshot.val();
        return admin.messaging().sendToDevice(userFcmToken, payload)
      })
      .then(res => {
        console.log("Sent Successfully", res);
        return null;
      })
      .catch(err => {
        console.log(err);
      });

推荐阅读