【问题标题】:When collection is created, the documents are added to it at once创建集合时,文档会立即添加到其中
【发布时间】:2020-07-10 09:16:10
【问题描述】:

仍在学习 firestore,我想编写一个函数,该函数将在一次创建集合时创建多个文档。所以我写了这段代码来尝试一下。我将代码视为类似问题的答案。

    const fsRef = admin.firestore();

export const moreCreations = functions.firestore
      .document(
        "dev_env/schools/school_collections/KithAndKin7394/students/{userID}"
      )
      .onCreate((snap, context) => {
        const newSchoolRef = fsRef
          .collection("dev_env")
          .doc("schools")
          .collection("school_collections")
          .doc("KithAndKin7394")
          .collection("students")
          .doc(snap.id);

         // Trying something on documents

         const documentIds = [
           'CRK_IRK',
           'PHE',
           'agricScience',
           'basicScience',
           'basicTechnology',
           'businessStudies',
           'computerStudies',
           'creativeArts',
           'english',
           'frenchLanguage',
           'hausaLanguage',
           'homeEconomics',
           'iboLanguage',
           'maths',
           'socialStudies',
           'yoruba'
         ];

         const batch = fsRef.batch();
         const data ={};
         const setbatch = documentIds.forEach(docId => {
          batch.set(newSchoolRef.collection('JSS1').doc('${docId}'), data);
         })
         batch. commit().then(response => {
           console.log('Success');
         }).catch(err => {
           console.error(err);
         })
      });

我收到以下错误:

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint C:\brighterbrains\functions
> tslint --project tsconfig.json


ERROR: C:/brighterbrains/functions/src/index.ts:168:23 - Expression has type `void`. Put it on its own line as a statement.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dell\AppData\Roaming\npm-cache\_logs\2020-03-30T02_04_34_455Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

我想要的是在创建集合“JSS1”时,立即将文档“documentsId”添加到其中。代码来自这个论坛,但它不起作用。请问有人能指出我的错误并帮助我更正吗?文档没有对此类行为做任何事情或说明任何事情。

提前致谢。

【问题讨论】:

  • 您的代码甚至还没有运行。 TypeScript 编译器说第 168 行有错误。请先查看。
  • 嗨,Doug,就像我之前说的,我是新手,我从这里获得了代码,这是链接:stackoverflow.com/questions/49012176/… 从他的代码中,他没有使用该 const在第 168 行
  • 请编辑问题以包含实际代码,并标记哪一行是 168,以便更容易看到您所看到的内容。我们不应该关注链接。
  • 我上传的代码是实际代码,只是documentsId数组略有变化。我向它添加了更多值,但代码是整个代码。
  • 第 168 行是哪一行?看起来问题中没有超过 50 行代码。请编辑问题以完全清楚编译器在抱怨哪一行。

标签: node.js typescript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

我已经解决了。以下是更新后的代码:

export const moreCreations = functions.firestore
  .document(
    "dev_env/schools/school_collections/KithAndKin7394/students/{userID}"
  )
  .onCreate((snap, context) => {
    const newSchoolRef = fsRef
      .collection("dev_env")
      .doc("schools")
      .collection("school_collections")
      .doc("KithAndKin7394")
      .collection("students")
      .doc(snap.id);

     // Trying something on documents

     const documentIds = [
       'CRK_IRK',
       'PHE',
       'agricScience',
       'basicScience',
       'basicTechnology',
       'businessStudies',
       'computerStudies',
       'creativeArts',
       'english',
       'frenchLanguage',
       'hausaLanguage',
       'homeEconomics',
       'iboLanguage',
       'maths',
       'socialStudies',
       'yoruba'
     ];

     const batch = fsRef.batch();
     const data ={};
      documentIds.forEach(docId => {
      batch.set(newSchoolRef.collection('JSS1').doc(docId), data);
     })
     batch.commit().then(response => {
       console.log('Success');
     }).catch(err => {
       console.error(err);
     })
  });

成功部署,创建文档后,自动创建集合JSS1,并自动添加documentsId数组下的文档。

我要感谢 @samthecodingman 帮助我找到我需要编辑的区域。还有@DougStevenson。

【讨论】:

  • 你也可以将 const newSchoolRef = ... 扁平化为 const newSchoolRef = snap.ref
【解决方案2】:

forEach() 之前删除const setbatch =。它没有返回值。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-06-11
  • 2020-11-13
  • 2019-08-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 2023-03-28
  • 2022-11-14
相关资源
最近更新 更多