我正在尝试从电子商务商店发送用户收据。我如何遍历数据进行发送
我曾尝试在动态数据上使用[]。
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
const db = admin.firestore();
// Sendgrid Config
import * as sgMail from "@sendgrid/mail";
const API_KEY = functions.config().sendgrid.key;
const TEMPLATE_ID = functions.config().sendgrid.template;
sgMail.setApiKey(API_KEY);
//FUNCTIONS
export const newOrder = functions.firestore
.document("checkout/{checkoutId}/products/{productId}")
.onCreate(async (change, context) => {
// Read booking document
const postSnap = await db
.collection("checkout/{checkoutId}/products")
.doc(context.params.productId)
.get();
const booking = postSnap.data() || {};
//Email
const msg = {
to: "wilmutsami@gmail.com",
from: "test@example.com",
templateId: TEMPLATE_ID,
dynamic_template_data: {
subject: "Hey there, thank you for your order!",
name: booking.name,
amount: booking.amount
}
};
//Send it
return sgMail.send(msg);
});
预期结果是给用户的电子邮件,显示您订购的项目表
最佳答案
如果要获取在checkout/{checkoutId}/products/{productId}
触发了Cloud Function的文档的数据,则无需执行此操作
await db
.collection("checkout/{checkoutId}/products")
.doc(context.params.productId)
.get();
如doc中所述:
触发功能后,它将提供数据快照
与事件有关。您可以使用此快照进行读取或写入
触发事件的文档,或使用Firebase管理员
SDK可以访问数据库的其他部分。
您可以通过
snap
DocumentSnapshot
轻松获得文档字段的值,如下所示:export const newOrder = functions.firestore
.document("checkout/{checkoutId}/products/{productId}")
.onCreate(async (snap, context) => {
const docData = snap.data();
const name = docData.name;
const amount = docData.amount;
// You can then use those values in the rest of your code
const msg = {
to: "wilmutsami@gmail.com",
from: "test@example.com",
templateId: TEMPLATE_ID,
dynamic_template_data: {
subject: "Hey there, thank you for your order!",
name: name,
amount: amount
}
};
return sgMail.send(msg);
});
关于node.js - 如何发送从Firestore数据库遍历数组的电子邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58506173/