簡體   English   中英

如何從 Cloud Functions 訪問和操作 Firebase 數據庫?

[英]How can I reach and manipulate Firebase Database from Cloud Functions?

我正在努力向用戶發送動態通知。 因此,我正在處理 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);
    }
});  

如您所見,首先,我想從 nofications 集合中獲取 email,然后使用該 email,我想搜索正在使用該 email 的用戶,之后,我想獲取用戶 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(內部/process/next_tick.js:229:7)

錯誤消息告訴您出了什么問題:

Reference.child 失敗:第一個參數是無效路徑 =“users/patientaccount@gmail.com/deviceToken”。 路徑必須是非空字符串,不能包含“.”、“#”、“$”、“[”或“]”

包含 email 地址的路徑包含無效字符. .

如果您想存儲每個用戶的數據,您應該使用用戶帳戶的唯一 ID。 由於各種原因,Email 地址並不是很好的唯一標識符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM