【问题标题】:Firebase Function not triggering on creation of new documentFirebase 功能未在创建新文档时触发
【发布时间】:2019-02-05 14:30:36
【问题描述】:

我编写了一个通过 twilio 发送文本的成功函数。这是我的第二次尝试。我希望它在创建文档时发送文本。我的函数显示在 Web 控制台中并通过了错误检查。检查日志,没有错误或触发记录。我怀疑我的路径是错误的,但我真的不知道问题是什么。 当我创建一个文档时,我使用它作为路径:

("rooms/" + myUserId + "/" + "thread")

这是我的 index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const db = admin.firestore()
const twilio = require('twilio');


const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = '+xxxxxxxxxx'

exports.sendNewChat = 
functions.database.ref('/rooms/{myUserId}/thread')
    .onCreate((snapshot, context) => {

      const textMessageNewChat = {
    body:'New Chat From: ' + sender,
    to:'xxxxxxxxxx',
    from: twilioNumber
  }
  return client.messages.create(textMessageNewChat)

    });

谢谢!

【问题讨论】:

  • 您确定您正确解释了控制台中的日志吗?尝试刷新页面。
  • 可以肯定的是,我删除了我的函数并重新部署了它。我在控制台中验证了删除和随后的重新部署。检查运行状况我得到“未发现错误”。我创建了一个文档,希望能触发该功能,但它没有。我确认该文档确实已创建。检查日志我今天没有活动,也没有该功能的活动。

标签: google-cloud-firestore google-cloud-functions


【解决方案1】:

您的问题被标记为 google-cloud-firestore,在您的问题中您提到了创建文档。所以你显然在这里与 Firestore 合作。但是您的函数是作为实时数据库触发器实现的,而不是 Firestore 触发器。您需要使用functions.firestore 而不是functions.database 进行构建。您应该查看Firestore triggers 的文档以了解如何构建它们。

【讨论】:

  • 嘿伙计,如何在集合上创建文档时发送文本?我无法到达它
【解决方案2】:

我遇到了同样的问题。花了很多时间发现firebase触发功能在Firebase上部署后正在执行。

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 2018-06-04
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多