【问题标题】:How to add a collection inside document on Firestore? React Native如何在 Firestore 上的文档中添加集合?反应原生
【发布时间】:2019-04-18 10:12:20
【问题描述】:

async componentDidMount() {

    firebase.firestore().collection('profiles').get().then(function(querySnapshot) {

        querySnapshot.forEach(function(doc) {

            var data = doc.data().pushToken; // I'm able to fetch pushToken here.

            const profileRef = firebase.firestore.doc(`profiles/0bT9eZ42CNN1uQYAE728WJht2aO2`)
                .collection('private').doc('pushToken');

            profileRef.set(data, {
                merge: true
            }).catch(e => console.error(e));

        });

    })
    .catch(function(error) {

        alert("error"); // My .then function throws an error and hence this alert works

        console.log("Error getting documents: ", error);
    });

}

我希望能够从每个文档的字段中获取pushToken,然后插入到同一个文档中名为private 的集合中。 但它给了我一个错误说

获取文档时出错:TypeError: _reactNativeFirebase2.default.firestore.doc 不是函数

如何将pushToken 移动到收藏夹?我想自动而不是手动地在每个文档中创建集合。

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    由于您已经有一个 DocumentSnapshot 用于您要在其下创建子集合的文档,您可以从该文档构建 CollectionReferenceDocumentReference 到新文档。

    这样的事情应该可以工作:

    const profileRef = doc.ref.collection('private').doc('pushToken');
    

    【讨论】:

    • 您现在可以查看我更新的问题吗?我现在已经回复了答案。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2018-07-22
    • 1970-01-01
    相关资源
    最近更新 更多