[英]On Update value of firestore document
我正在嘗試將聊天應用程序作為學習項目。 當我試圖跑步時
firebase.functions.firestore
.document('messages/lastMessage')
.onUpdate((snapshot, context) => {
const after = snapshot.after.data();
console.log(after)
})
我收到此錯誤: Uncaught TypeError: Cannot read property 'document' of undefined
錯誤: Uncaught TypeError: Cannot read property 'document' of undefined
這就是我將 firebase 導入到我的 html 中的方式:
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instter</title>
<script:scr="https://www.gstatic.com/firebasejs/8.0.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.7.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.7.0/firebase-functions.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.7.0/firebase-firestore.js"></script>
</head>
<body>
<script src="index.js"></script>
</body>
我試過到處搜索,但沒有找到任何東西。 如果您對此有所了解,請告訴我。 謝謝
您似乎正在嘗試在您的網絡應用程序中運行這些功能。 您必須使用項目目錄中的 CLI 初始化 Cloud Functions。 創建一個新目錄(或使用現有的 Firebase 項目目錄)並運行以下命令:
firebase init functions
完成設置,您將獲得與此類似的目錄結構(Javascript 和 Typescript 略有不同):
然后在您的index.ts
(或.js
)文件中:
import * as functions from "firebase-functions";
export const firestoreTrigger = functions.firestore.document("/messages/{lastMessage}").onUpdate((change, context) => {
const lastMessage = change.after.data().lastMessage;
console.log(lastMessage);
return;
});
您可以在其中編寫函數。 要進行部署,請運行firebase deploy --only functions
命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.