Cloud Function's Cloud Firestore trigger

Kazuaki Suzuki

I use Firebase and I would like to use Cloud Function's Cloud Firestore triggers. https://firebase.google.com/docs/functions/firestore-events

I have made a simple web app to test Cloud Function's Cloud Firestore triggers.

But Cloud Functions don't work.

Could you give me any advices? Thank you in advance.


When I push download button from browser, I get Firestore response. firestore

However, I don't get Functions response. There is no console.log's "Hello Trigger!" functions

This is my directory structure.

.
├── firebase.json
├── firestore.indexes.json
├── firestore.rules
├── functions
│   ├── index.js
│   ├── package.json
│   └── package-lock.json
├── public
│   ├── index.html
│   └── scripts
│       └── main.js
└── storage.rules

This is index.js.

<!doctype html>
<html lang="ja">
   <body>
      <textarea id="text" class="form-control" name="text" placeholder="ここに英文を入力してください。" maxlength="3000" minlength="1"></textarea>
      <br>
      <div style="text-align:center">
        <input id="download" class="btn btn-primary" type="submit" value="音声をダウンロード">
      </div>

      <script src="/__/firebase/7.14.3/firebase-app.js"></script>
      <script src="/__/firebase/7.14.3/firebase-auth.js"></script>
      <script src="/__/firebase/7.14.3/firebase-storage.js"></script>
      <script src="/__/firebase/7.14.3/firebase-messaging.js"></script>
      <script src="/__/firebase/7.14.3/firebase-firestore.js"></script>
      <script src="/__/firebase/7.14.3/firebase-performance.js"></script>
      <script src="/__/firebase/init.js"></script>

      <script src="scripts/main.js"></script>
   </body>
</html>

main.js

'use strict';

// Saves a new message on the Cloud Firestore.
function saveMessage() {
  // Add a new message entry to the Firebase database.
  return firebase.firestore().collection('messages').add({
    text: messageInputElement.value,
    timestamp: firebase.firestore.FieldValue.serverTimestamp()
  }).catch(function(error) {
    console.error('Error writing new message to Firebase Database', error);
  });
}

// Checks that the Firebase SDK has been correctly setup and configured.
function checkSetup() {
  if (!window.firebase || !(firebase.app instanceof Function) || !firebase.app().options) {
    window.alert('You have not configured and imported the Firebase SDK. ' +
        'Make sure you go through the codelab setup instructions and make ' +
        'sure you are running the codelab using `firebase serve`');
  }
}

// Checks that Firebase has been imported.
checkSetup();

// Shortcuts to DOM Elements.
var messageInputElement = document.getElementById('text');
var submitButtonElement = document.getElementById('download');

// Saves message on form submit.
submitButtonElement.addEventListener('click', saveMessage);

index.js

const functions = require('firebase-functions');

exports.myFunction = functions.firestore
  .document('messages/messages')
  .onCreate((change, context) => {
    console.log("Hello Trigger!");
    return 0;   

  });
Doug Stevenson

You function is set to trigger on the creation of a document called exactly "messages/messages". That means it will only work if you create a message with the ID "messages" in a collection called "messages". I'm pretty sure that's not what you want. If you want to trigger on any new document in a collection called messages, you need a wildcard:

exports.myFunction = functions.firestore
  .document('messages/{id}')
  .onCreate((change, context) => { ... })

I suggest reviewing the documentation to learn about wildcards in the trigger path.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cloud Firestore trigger for Cloud Function to invoke Composer

Firestore cloud function trigger with wildcard path

how to use reusable code for Google Cloud Function Firestore trigger?

Can't get onCreate function to trigger using TypeScript on Cloud Firestore

How to trigger a cloud function based on a timestamp attribute of a firestore document

Firebase - Cloud Firestore Trigger Idempotency

Delete document in Firestore with Cloud Firestore update trigger

Trigger Firebase Cloud Function on inserting to Cloud Firestore document for the first time with given uid

Firebase : Firestore not updating data in Cloud Function's scheduled function

Cloud function trigger for multiple collections

cloud function trigger on create document

Cloud function trigger on document update

Trigger Firebase Cloud Function on linkWithCredential()

How to access Cloud Firestore from a Cloud Function?

Query from Cloud Firestore by timestamp in Cloud Function

Access a cloud firestore document within a cloud function

Configuring cloud Firestore beta with cloud Firebase function

Cloud Functions and Cloud Firestore USER ASSOCIATION FUNCTION

Cloud firestore trigger query by documentID for array of ids

Firestore data trigger events in Cloud Run

Run batch or transaction in Cloud Firestore onCreate trigger

Cloud Functions, Firestore trigger, recursive delete not working

How do Firestore (authentication) rules see cloud function reads/writes and trigger function reads/writes?

Why is the select() function not available in Flutter's cloud_firestore package?

Firestore cloud function, add author's UID to document creation

Error with Firestore update in a Cloud Function

Get Document in Firestore Cloud Function

Cannot deploy Firestore Cloud Function

Firestore Cloud Function empty collection