【问题标题】:How to create collections in cloud firestore dynamically from cloud functions如何从云函数动态地在 Cloud Firestore 中创建集合
【发布时间】:2018-11-24 23:19:00
【问题描述】:

我正在尝试从云功能动态地为集合内的文档创建集合和子集合, 但我得到以下异常 **

参数“collectionPath”必须指向一个集合,但是 “数据/c2f7c4e84366”。您的路径不包含奇数个 组件

**

日志 -

错误:参数“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)

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

如果不存在,我希望云函数在导出集合(已存在)中动态创建子集合(userid_inbox)并添加配置文件。

【问题讨论】:

  • 请编辑您的问题以显示产生此错误的代码。

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


【解决方案1】:

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

使用此代码,您正在创建一个新文档,其中包含新子集合上的值消息。

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2018-05-05
    • 2021-06-04
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多