【问题标题】:Firebase function is not getting triggred. Integration with sendgridFirebase 功能没有被触发。与 sendgrid 集成
【发布时间】:2018-03-09 07:04:15
【问题描述】:

我正在编写一个如下的 firebase 函数。问题是当我添加新用户时它没有被触发。请告诉我我在哪里做错了。 我对以下功能的例外是,每当一个条目转到/user 文档时,一封电子邮件应该转到用户电子邮件。基本上它是一个注册页面,为了成功注册,我想发送电子邮件。

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as sendgrid from '@sendgrid/mail';


admin.initializeApp(functions.config().firebase);
// const SENDGRID_API_KEY = functions.config().sendgrid.key;
const SENDGRID_API_KEY = 'SG.xxxxCfXA46A.6i-lhxxxxxxxxxxx0';
// const sendgrid = require('@sendgrid/mail');

sendgrid.setApiKey(SENDGRID_API_KEY);



exports.userCreateEmail = functions.database.ref('/users/{userId}').onCreate((event) => {
    const userId = event.params.userId;
    console.info('userId '+userId);
    const db = admin.firestore();
    return db.collection('users').doc(userId).get().then(doc => {
        const user = doc.data();
        console.log(user);
        const msg = {
            to: user.email,
            from: 'asdf@gmail.com',
            subject: 'new follower',
            templateId: 'b98f5d37-8371-4c24-850c-sdfasdf'
        };
        return sendgrid.send(msg);
    }).then(() => console.log('email sent!'))
        .catch(err => console.log(err))
});

【问题讨论】:

  • 请编辑您的问题以准确解释它应该做什么,以及您所做的更改期望它被调用。特别是,您显然在等待实时数据库中的更改,然后在 Firestore 中进行工作,这没有任何意义。

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


【解决方案1】:

如果使用 Firebase:

改变这个:

exports.userCreateEmail = functions.database.ref(`/users`).onCreate((event) => {

到这里:

exports.userCreateEmail = functions.database.ref('/users/{userId}').onCreate((event) => {

也使用' 代替`

另外event.params 将检索通配符的值,因此您必须将{//anyname} 添加为通配符,然后您才能检索它。

还可以使用val() 而不是data() 检索数据

如果你使用的是 firestore,那么你需要使用这个:

exports.userCreateEmail = functions.firestore.document('users/{userId}').onCreate((event) => {

【讨论】:

  • 我根据您的评论进行了更改,仍然遇到同样的问题。
  • 确保文档名称正确,“用户”应与控制台@VijayKeshri中的文档名称相同
  • 是的,我正在使用 Firestore 云,如果我正在更改 export.userCreateEmail = functions.firestore.document('/users/{userId}').onCreate( (event) => {
  • 这个console.info('userId '+userId); 是否出现在控制台中?
  • 1:49:35.796 PM info userCreateEmail { 错误:getaddrinfo ENOTFOUND api.sendgrid.com api.sendgrid.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) 代码:'ENOTFOUND',errno:'ENOTFOUND',系统调用:'getaddrinfo',主机名:'api.sendgrid.com',主机:'api.sendgrid.com',端口: 443 }
猜你喜欢
  • 2020-12-23
  • 1970-01-01
  • 2021-09-14
  • 2019-06-24
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多