1
votes

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;   

  });
2

2 Answers

4
votes

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.

2
votes

In index.html add cloud functions script because it is missing :

<script src="/__/firebase/7.14.3/firebase-functions.js"></script>

Don't forget to deploy your functions : go to the project directory and

npm deploy --only functions