创建集合后,文档会立即添加到其中

时间:2020-03-30 02:21:54

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

仍在学习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”添加到其中。该代码来自该论坛,但无法正常工作。请谁能指出我的错误并帮助我进行更正?该文档没有对此类操作进行任何说明或说任何话。

谢谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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,并自动在documentId数组下添加文档。

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