【问题标题】:How can I reach and manipulate Firebase Database from Cloud Functions?如何从 Cloud Functions 访问和操作 Firebase 数据库?
【发布时间】:2020-09-01 22:13:17
【问题描述】:

我正在向用户发送动态通知。因此,我正在处理 Firebase Cloud Functions。 这是我的问题:如何在云功能端访问数据库?

这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');




// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

admin.initializeApp(functions.config().functions);


var newData;



//exports.myTrigger= functions.firestore.document().




exports.notificationTrigger = functions.firestore.document(`notifications/{notificationId}`).onCreate(async (snapshot, context) => {


    if (snapshot.empty) {
        console.log('No Devices');
        return;
    }
  var notificationData = snapshot.data();
  // notificationData.email;
  console.log(`Data  is : ${notificationData.email}`);


      admin.database().ref(`users/${notificationData.email}/deviceToken`).then((snapshot) => {
            var token = snapshot.data();
            console.log('token',token)
            return 1;
        }).catch(err => {
            console.error(err);

        }); 





    var tokens = [
        `${token.deviceToken}`
    ];



    var payload = {
        notification: {
            title: `Yeni Eklenen Değerin Maili : ${notificationData.email}`,
            body: 'hüsen',
            sound: 'default',
        },
        data: {
            click_action: 'FLUTTER_NOTIFICATION_CLICK',
            Messages:'Baba Mesajı from otomatik fonksiyon'
        },
    };

    try {
        const response = await admin.messaging().sendToDevice(tokens, payload);
        console.log('Notification sent successfully');
    } catch (err) {
        console.log(err);
    }
});  

如您所见,首先,我想从通知集合中获取电子邮件,然后使用该电子邮件,我想搜索正在使用该电子邮件的用户,然后,我想获取用户 deviceToken。

我尝试了很多关于语法的东西,但没有得到任何结果:/

同样,我犯了一些错误:

您可以在此处查看错误:

  • Reference.child 失败:第一个参数是无效路径 =“users/patientaccount@gmail.com/deviceToken”。路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]” 在 validatePathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1667:15) 在 validateRootPathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1679:5) 在 Reference.child (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:13843:17) 在 Database.ref (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:15097:48) 在exports.notificationTrigger.functions.firestore.document.onCreate (/srv/index.js:37:24) 在 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) 在 /worker/worker.js:825:24 在 在 process._tickDomainCallback (internal/process/next_tick.js:229:7)

【问题讨论】:

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


    【解决方案1】:

    错误信息告诉你出了什么问题:

    Reference.child 失败:第一个参数是无效路径 =“users/patientaccount@gmail.com/deviceToken”。路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]”

    包含电子邮件地址的路径包含无效字符 .。

    如果您想存储每个用户的数据,您应该使用用户帐户的唯一 ID。由于各种原因,电子邮件地址不能成为很好的唯一标识符。

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2020-02-01
      • 2017-09-24
      • 2023-04-06
      • 2018-05-31
      • 2018-06-29
      相关资源
      最近更新 更多