繁体   English   中英

关于 Firestore 文档的更新值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM