尽管添加了触发器

时间:2018-03-28 23:08:10

标签: email firebase google-cloud-functions sendgrid sendgrid-api-v3

我有以下云功能,尝试使用sendgrid在新用户注册时发送电子邮件。这是关于如何使用nodemailer发送电子邮件的原始firebase repo的修改版本。这在早期工作正常,没有sendgrid功能。因此,您在下面看到的是代码的大量修改版本。

'use strict';

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
const SEND_GRID_API_KEY = functions.config().sengrid.key


const sgMail = require('@sengrid/mail');
sgMail.setApiKey(SEND_GRID_API_KEY);




// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = 'Asia Rubber';

// [START sendWelcomeEmail]
/**
 * Sends a welcome email to new user.
 */
// [START onCreateTrigger]
exports.sendWelcomeEmail = functions.auth.user().onCreate((event) => {
  // [END onCreateTrigger]
  // [START eventAttributes]
  const user = event.data; // The Firebase user.

  const email = user.email; // The email of the user.
  const displayName = user.displayName; // The display name of the user.
  // [END eventAttributes]

  return sendWelcomeEmail(email, displayName);
});
// [END sendWelcomeEmail]

// [END sendByeEmail]

// Sends a welcome email to the given user.
function sendWelcomeEmail(email, displayName) {
  const msg = {
    from: `${APP_NAME} <Welcome@rubber.asia>`,
    to: email,
    subject: 'Welcome to ${APP_NAME}!',


    //custom templates
    templateId: 'e3978a51-5343-4b3a-9128-8c4a493f265e'
  };
  return sgMail.send(msg)
  .then( () => console.log('email sent'))
.catch(err => console.log(err))
}

当我尝试部署时,我收到错误

PS E:\Cloudfunctions\erubberFunctions\functions> firebase deploy --only functions

=== Deploying to 'erubber'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint E:\Cloudfunctions\erubberFunctions\functions
> eslint .


E:\Cloudfunctions\erubberFunctions\functions\index.js
  79:14  warning  Unexpected template string expression  no-template-curly-in-string

✖ 1 problem (0 errors, 1 warning)

+  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

TypeError: Cannot read property 'key' of undefined
    at Object.<anonymous> (E:\Cloudfunctions\erubberFunctions\functions\index.js:5:53)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at C:\Users\bhara\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:18:11
    at Object.<anonymous> (C:\Users\bhara\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:38:3)

所以,从我的代码中我唯一的密钥是sendgrid api密钥,我已经像这样添加了

firebase functions:config:set sendgrid.key=SG.YOUR_API_KEY

键没有引号btw。

我做错了什么? 一般来说,代码一般有什么问题吗? TKS

1 个答案:

答案 0 :(得分:2)

你的云功能有一个小错字。您正在尝试访问该媒体资源sengrid(在d中遗漏send)。

正确的代码行应该是;

const SEND_GRID_API_KEY = functions.config().sendgrid.key