Cloud Function的Cloud Firestore触发器

编程入门 行业动态 更新时间:2024-10-11 13:18:53

Cloud Function的Cloud Firestore<a href=https://www.elefans.com/category/jswz/34/1768873.html style=触发器"/>

Cloud Function的Cloud Firestore触发器

我使用Firebase,我想使用Cloud Function的Cloud Firestore触发器。

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

但是云功能不起作用。

您能给我提些建议吗?预先谢谢你。


[当我从浏览器中按下下载按钮时,我得到了Firestore的响应。

但是,我没有收到功能答复。没有console.log的“ Hello Trigger!”。

这是我的目录结构。

.
├── 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>

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;   

  });
回答如下:

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

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

我建议复习documentation以了解触发路径中的通配符。

更多推荐

Cloud Function的Cloud Firestore触发器

本文发布于:2024-05-07 21:13:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   Cloud   Function   Firestore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!