【发布时间】:2019-06-07 01:50:17
【问题描述】:
在这些示例之后,我正在尝试使用谷歌云功能发送电子邮件: https://github.com/firebase/functions-samples/tree/master/quickstarts/email-users 和https://angularfirebase.com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/ 然后我不得不进行一些调整以适应发展中的技术,我的结果是:
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const sendGrid = require('@sendgrid/mail');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const SENDGRID_API_KEY="***";
sgMail.setApiKey(SENDGRID_API_KEY);
sendGrid.setApiKey(functions.config().sendgrid.apikey);
sendGrid.setSubstitutionWrappers('{{', '}}');
exports.httpEmail = functions.https.onRequest((req, res) => {
cors( req, res, () => {
const name = req.body.toName;
const email = req.body.Email;
const id = req.body.ID;
const msg = {
to: email,
from: mail@homepage.com',
subject: id,
text: `Hello {{name}}`,
html: `<h1>Hello {{name}}</h1>`
};
return sgMail.send(msg)
.then(() => res.status(200).send('email sent!') )
.catch(err => res.status(400).send(err) )
});});
现在这给了我一个语法错误
/user_code/index.js:20 to: {{email}}, ^ SyntaxError: Unexpected token
{
"name": "email",
"description": "sending emails",
"version": "0.0.1",
"dependencies": {
"sendgrid/mail": "^6.3.1",
"cors": "^2.8.1",
"moment": "^2.17.1",
"firebase-admin": "~5.11.0",
"firebase-functions": "^1.0.0"
},
"scripts": {
"lint": "./node_modules/.bin/eslint --max-warnings=0 .",
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"private": true
}
这给了我一个解析错误:
无法解析 package.json,第 7 行第 6 列出错
这两个错误对我来说都没有意义,我不得不做出 4 种不同的调整,这让我怀疑我是否走在正确的道路上。任何人都有一个工作示例或教程,包括 cors、带变量的文本和 sendgrid?或者有人可以为我解决错误信息吗?
【问题讨论】:
标签: google-cloud-functions sendgrid