创建收藏集后,文档会立即添加到其中 [英] When collection is created, the documents are added to it at once

查看:32
本文介绍了创建收藏集后,文档会立即添加到其中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Still learning firestore, i want to write a function which would create multiple documents upon the creation of a collection at once. So i wrote this code to try it out. i saw the code as an answer to something similar.

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

What i want is when the collection "JSS1" is created, the documents "documentsId" are added to it at once. The code is from this forum but it is not working. Please can anyone point my mistake and help with me with the corrections? The documentation doesn't do or say anything about such actions.

谢谢.

推荐答案

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

Remove const setbatch = from before the forEach(). It doesn't have a return value.

这篇关于创建收藏集后,文档会立即添加到其中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆