我正在使用 express 創建我的 firebase 函式,并且我了解如何創建常規的可呼叫函式。然而,我迷失了為后臺實作觸發功能的確切方式(即 onCreate、onDelete、onUpdate、onWrite),以及前端的 Reactjs 應該如何接收資料。
我的場景是一個通用的聊天系統,它使用帶有快速和實時資料庫的 React、Firebase 函式。我通常對在有人發送訊息時使用觸發器來更新另一個用戶的前端資料的程序感到困惑。
我很難找到有關這些問題組合的教程或檔案。生命周期的任何鏈接或基本程式示例都會很棒。
我理解的部分是撰寫觸發函式的方式:
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite((change, context) => {
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
// Grab the current value of what was written to the Realtime Database.
const original = change.after.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return change.after.ref.parent.child('uppercase').set(uppercase);
});
但我不明白這是如何被呼叫的,也不知道資料是如何到達前端代碼的。
uj5u.com熱心網友回復:
后臺函式不能向客戶端回傳任何內容。他們在某個事件之后運行,即onWrite()在這種情況下。如果您想將資料更新/messages/{pushId}/original到其他用戶,則必須使用Firebase Client SDK來監聽該路徑:
import { getDatabase, ref, onValue} from "firebase/database";
const db = getDatabase();
const msgRef = ref(db, `/messages/${pushId}/original`);
onValue(msgRef, (snapshot) => {
const data = snapshot.val();
console.log(data)
});
您還可以收聽/messages/${pushId}withonChildAdded()以獲取有關該路徑下任何新節點的通知。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534597.html
標籤:Google Cloud Collective 反应火力基地表示firebase-实时数据库谷歌云功能
上一篇:無法發布;不能用反應軸制作帖子
