[英]Firestore Cloud Function - Send E-Mail onCreate with SendGrid
I have a contact form which submits data to the Firestore Database. 我有一个联系表,可以将数据提交到Firestore数据库。 My intention is, that as soon as there's another entry in the collection
requests
, Firestore shall fire a function via Cloud Function, which contains the configuration for SendGrid, which again is supposed to send the data of this specific entry to an e-mail. 我的意图是,只要收集
requests
有另一个条目,Firestore就会通过Cloud Function触发一个函数,该函数包含SendGrid的配置,该配置又应将此特定条目的数据发送到电子邮件。
I've also tried to deploy this function, which was successful - but the console shows the following errors, which I reckon won't be the only one: 我还尝试部署了此功能,该功能很成功-但控制台显示了以下错误,我认为这不是唯一的错误:
Cannot read property 'requestId' of undefined
无法读取未定义的属性“ requestId”
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.firestoreEmail = functions.firestore
.document('requests/{requestId}')
.onCreate(event => {
const requestId = event.params.requestId;
const db = admin.firestore();
return db.collection('requests').doc(requestId)
.get()
.then(doc => {
const requestId = event.params.requestId;
const request = doc.data();
const msg = {
to: 'fuh@gmx.net',
from: 'hello@angularfirebase.com',
templateId: 'd-',
substitutionWrappers: ['{{', '}}'],
substitutions: {
name: request.name,
lastname: request.lastname,
email: request.email,
package: request.package,
date: request.date,
text: request.text
// and other custom properties here
}
};
return sgMail.send(msg)
})
.then(() => console.log('email sent!') )
.catch(err => console.log(err) )
});
Edit: 编辑:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.request = functions.firestore
.document('requests/{requestId}')
.onCreate((snap, context) => {
const db = admin.firestore();
return db.collection('requests').doc(requestId)
.get()
.then(doc => {
const requestId = snap.id;
const request = doc.data();
const msg = {
to: 'fuhr@gmx.net',
from: 'hello@angularfirebase.com',
templateId: 'd-3cd6b40ad6674f33702107d2',
substitutionWrappers: ['{{', '}}'],
substitutions: {
name: request.name,
lastname: request.lastname,
email: request.email,
package: request.package,
date: request.date,
text: request.text
// and other custom properties here
}
};
return sgMail.send(msg)
})
.then(() => console.log('email sent!') )
.catch(err => console.log(err) )
});
The .onCreate()
method doesn't return event
, it returns the snapshot of the object and from it you get the id of the new object. .onCreate()
方法不返回event
,它返回对象的快照,并从中获取新对象的ID。
So in your case, it has to be: 因此,在您的情况下,必须为:
exports.firestoreEmail = functions.firestore.document('requests/{requestId}')
.onCreate((snap, context) => {
const requestId = snap.id; // get the id
const db = admin.firestore();
return db.collection('requests').doc(requestId)
.get()
.then(doc => {
const request = doc.data();
const msg = {
to: 'fuhr@gmx.net',
from: 'hello@angularfirebase.com',
templateId: 'd-3cd6b40ad6674f33702107d2',
substitutionWrappers: ['{{', '}}'],
substitutions: {
name: request.name,
lastname: request.lastname,
email: request.email,
package: request.package,
date: request.date,
text: request.text
// and other custom properties here
}
};
return sgMail.send(msg)
})
.then(() => console.log('email sent!') )
.catch(err => console.log(err) )
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.