我有以下云功能,它在“student_history”集合中为“学生”集合中的每一个新文档创建一个文档:
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()});
});我想把这个概括成另外两个系列。就像这样:
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()
});
}
}https://stackoverflow.com/questions/72497257
复制相似问题