简体   繁体   English

Cloud Function 的 Cloud Firestore 触发器

[英]Cloud Function's Cloud Firestore trigger

I use Firebase and I would like to use Cloud Function's Cloud Firestore triggers.我使用 Firebase 并且我想使用 Cloud Function 的 Cloud Firestore 触发器。 https://firebase.google.com/docs/functions/firestore-events https://firebase.google.com/docs/functions/firestore-events

I have made a simple web app to test Cloud Function's Cloud Firestore triggers.我制作了一个简单的 web 应用程序来测试 Cloud Function 的 Cloud Firestore 触发器。

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!"没有console.log的“Hello Trigger!” 功能

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.这是 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 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 index.js

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

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

  });

You function is set to trigger on the creation of a document called exactly "messages/messages".您将 function 设置为在创建一个名为“消息/消息”的文档时触发。 That means it will only work if you create a message with the ID "messages" in a collection called "messages".这意味着只有在名为“messages”的集合中创建 ID 为“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:如果要在名为 messages 的集合中触发任何新文档,则需要一个通配符:

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

I suggest reviewing the documentation to learn about wildcards in the trigger path.我建议查看文档以了解触发路径中的通配符。

In index.html add cloud functions script because it is missing:在 index.html 添加云函数脚本,因为它缺少:

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

Don't forget to deploy your functions: go to the project directory and不要忘记部署你的功能: go 到项目目录和

npm deploy --only functions

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

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