javascript - Cloud Functions Cloud Firestore 触发器

标签 javascript node.js firebase google-cloud-firestore google-cloud-functions

我使用 Firebase,我想使用 Cloud Functions 的 Cloud Firestore 触发器。 https://firebase.google.com/docs/functions/firestore-events

我制作了一个简单的网络应用来测试 Cloud Function 的 Cloud Firestore 触发器。

但是 Cloud Functions 不起作用。

你能给我一些建议吗? 提前谢谢你。


当我从浏览器按下下载按钮时,我收到 Firestore 响应。 firestore

但是,我没有收到 Functions 响应。 没有 console.log 的“Hello Trigger!” functions

这是我的目录结构。

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

这是 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>

主要.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);

索引.js

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

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

  });

最佳答案

您的函数设置为在创建名为“消息/消息”的文档时触发。这意味着只有当您在名为“messages”的集合中创建 ID 为“messages”的消息时,它才会起作用。我很确定那不是你想要的。如果你想在一个名为 messages 的集合中的任何新文档上触发,你需要一个通配符:

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

我建议查看 documentation了解触发器路径中的通配符。

关于javascript - Cloud Functions Cloud Firestore 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61905431/

相关文章:

javascript - 无法读取未定义的属性“主体”

javascript - 如何防止 Firebase 请求伪造?

javascript - 错误 : Query. 失败:第一个参数必须是对象

javascript - 在 Firebase Cloud Functions 中使用 Google Vision API 时出现类型错误

javascript - 具有相同名称和不同 id 的选择字段数组。

javascript - 使用 Harp 的 SQLITE 连接

node.js - 转发到另一个路由处理程序而不在 Express 中重定向

javascript - 使用 vanillaJS 无限绘制/取消绘制 SVG 路径循环

javascript - 循环延迟超时问题

node.js - 这是什么意思 NODE_PATH=$NODE_PATH :./共享 Node --harmony