首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何分别定义集合名称并将其传递给Firebase函数?

如何分别定义集合名称并将其传递给Firebase函数?
EN

Stack Overflow用户
提问于 2022-06-04 05:15:16
回答 2查看 74关注 0票数 1

我有以下云功能,它在“student_history”集合中为“学生”集合中的每一个新文档创建一个文档:

代码语言:javascript
运行
复制
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()});
  });

我想把这个概括成另外两个系列。就像这样:

代码语言:javascript
运行
复制
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?

代码语言:javascript
运行
复制
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()
    });
  }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72497257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档