如何单独定义集合名称并将其传递给 Firebase 云函数?
How to separately define and pass a collection name to a Firebase Cloud Function?
我有以下云函数,它在 'student_history' 集合中为 'students' 集合中的每个新文档创建创建一个文档:
document("students/{student_id}").onCreate(
async (snap, context) => {
const values = snap.data();
console.log(values);
console.log(typeof values);
return db.collection("student_history").add({...values, createdAt:FieldValue.serverTimestamp()});
});
我想将其推广到其他 2 个系列。像这样:
export const onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated);
export const onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated);
export const onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);
我的问题是,我怎样才能让我的 onDocCreated 函数接收集合名称(例如,学生、批次或教师)并输入相应的 students_history、batches_history 或 teachers_history?
async function onDocCreated() {
async (snap, context) => {
const values = snap.data();
console.log(values);
console.log(typeof values);
return db.collection("NAMEOFTHECOLLECTION_history").add({
...values,
createdAt: FieldValue.serverTimestamp()
});
}
}
首先,您需要在 onDocCreated()
函数本身中传递 snap
和 context
参数。 snap
是一个 QueryDocumentSnapshot,因此您可以使用从 parent
属性 获取集合 ID,如下所示:
async function onDocCreated(snap, context) {
const values = snap.data();
console.log(values);
const collectionName = snap.ref.parent.id;
console.log("Collection Name:", collectionName)
return db.collection(`${collectionName}_history`).add({
...values,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
});
}
添加到@Dharamaj 的回答,
按照他的建议
export const onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated);
export const onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated);
export const onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);
需要替换为:
exports.onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated);
exports.onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated);
exports.onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);
我有以下云函数,它在 'student_history' 集合中为 'students' 集合中的每个新文档创建创建一个文档:
document("students/{student_id}").onCreate(
async (snap, context) => {
const values = snap.data();
console.log(values);
console.log(typeof values);
return db.collection("student_history").add({...values, createdAt:FieldValue.serverTimestamp()});
});
我想将其推广到其他 2 个系列。像这样:
export const onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated);
export const onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated);
export const onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);
我的问题是,我怎样才能让我的 onDocCreated 函数接收集合名称(例如,学生、批次或教师)并输入相应的 students_history、batches_history 或 teachers_history?
async function onDocCreated() {
async (snap, context) => {
const values = snap.data();
console.log(values);
console.log(typeof values);
return db.collection("NAMEOFTHECOLLECTION_history").add({
...values,
createdAt: FieldValue.serverTimestamp()
});
}
}
首先,您需要在 onDocCreated()
函数本身中传递 snap
和 context
参数。 snap
是一个 QueryDocumentSnapshot,因此您可以使用从 parent
属性 获取集合 ID,如下所示:
async function onDocCreated(snap, context) {
const values = snap.data();
console.log(values);
const collectionName = snap.ref.parent.id;
console.log("Collection Name:", collectionName)
return db.collection(`${collectionName}_history`).add({
...values,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
});
}
添加到@Dharamaj 的回答,
按照他的建议
export const onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated);
export const onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated);
export const onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);
需要替换为:
exports.onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated);
exports.onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated);
exports.onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);