简体   繁体   中英

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

this is how i am importing firebase into my 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. Create a new directory (or use existing Firebase project directory) and run the following command:

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):

在此处输入图片说明

Then in your index.ts (or .js ) file:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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