简体   繁体   English

关于 Firestore 文档的更新值

[英]On Update value of firestore document

I am trying to make a chat app as a learning project.我正在尝试将聊天应用程序作为学习项目。 when i am trying to run当我试图跑步时

firebase.functions.firestore
    .document('messages/lastMessage')
    .onUpdate((snapshot, context) => {

        const after = snapshot.after.data();
        console.log(after)

})

i am getting this error: Uncaught TypeError: Cannot read property 'document' of undefined我收到此错误: Uncaught TypeError: Cannot read property 'document' of undefined错误: Uncaught TypeError: Cannot read property 'document' of undefined

this is how i am importing firebase into my html:这就是我将 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>

I have tried searching everywhere but didn't found anything.我试过到处搜索,但没有找到任何东西。 please let me know if you know something about this.如果您对此有所了解,请告诉我。 Thank you谢谢

It seems like you are trying to run the functions in your web app.您似乎正在尝试在您的网络应用程序中运行这些功能。 You must initialize Cloud Functions using the CLI in your project directory.您必须使用项目目录中的 CLI 初始化 Cloud Functions。 Create a new directory (or use existing Firebase project directory) and run the following command:创建一个新目录(或使用现有的 Firebase 项目目录)并运行以下命令:

firebase init functions

Go through the setup and you'll get a directory structure similar to this (will be slightly different for Javascript and Typescript):完成设置,您将获得与此类似的目录结构(Javascript 和 Typescript 略有不同):

在此处输入图片说明

Then in your index.ts (or .js ) file:然后在您的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;
});

You can write your functions in them.您可以在其中编写函数。 To deploy, run the firebase deploy --only functions command.要进行部署,请运行firebase deploy --only functions命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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