gpt4 book ai didi

node.js - 如何从 firestore 数据库发送循环遍历数组的电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:07 26 4
gpt4 key购买 nike

我正在尝试从电子商务商店发送用户收据。如何循环发送数据

我尝试过在动态数据上使用 []。

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} 获取触发云函数的文档数据,则无需执行

await db
.collection("checkout/{checkoutId}/products")
.doc(context.params.productId)
.get();

doc 中所述:

When a function is triggered, it provides a snapshot of the data related to the event. You can use this snapshot to read from or write to the document that triggered the event, or use the Firebase Admin SDK to access other parts of your database.

您可以通过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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com