如何从云功能动态创建云Firestore中的集合

时间:2018-06-15 06:11:57

标签: firebase google-cloud-firestore google-cloud-functions

我正在尝试从云功能动态创建集合中的文档集合和子集合, 但我得到以下例外 **

  

参数" collectionPath"必须指向一个集合,但是   "数据/ c2f7c4e84366&#34 ;.您的路径不包含奇数   部件

**

日志 -

错误:参数" documentPath"必须指向一个文档,但是" exports / user343434_inbox / profile"。您的路径不包含偶数个组件。     在Firestore.doc(/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/index.js:282:13)     在admin.firestore.doc.set.then.ws(/user_code/lib/index.js:28:53)

代码段: const p = admin.firestore()。doc(exports/${userInboxCollectionName}/profile)。set(reqData,{merge:false}) *

我期待云功能在导出集合(已存在)内动态创建子集合(userid_inbox),如果不存在,则添加配置文件。

1 个答案:

答案 0 :(得分:0)

Firestore的工作方式如下:collection / document / subCollection / "subDocument" /,依此类推。

您应该尝试:(我正在使用TS

let p = admin.firestore().collection(`exports/${userUID}/${theSubcollection}`)
    .add({ message: messageString })
    .then(ref => {
        console.log('Added document with ID: ', ref.id)
});

//let's see the estructure here:
//collection.......exports   
//document.........${userUID}
//subcollection....${theSubcollection}
//document.........ref.id
//data.............message with value messageString

使用此代码,您将在新的子集合上创建带有值消息的新文档。