如何在Firestore上的文档内部添加集合?反应本机 [英] How to add a collection inside document on Firestore? React Native

查看:50
本文介绍了如何在Firestore上的文档内部添加集合?反应本机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 的同一文档中的集合中.但这给了我一个错误

I want to be able to fetch pushToken from each document's field and then insert into a collection within the same document by the name private. But it gives me an error saying

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

Error getting documents: TypeError: _reactNativeFirebase2.default.firestore.doc is not a function

如何将 pushToken 移至集合?我想自动而不是手动地在每个文档中创建集合.

How can I move pushToken to a collection? And I want to make the collection inside each document automatically and not manually.

推荐答案

因为您已经有了要在其下创建子集合的文档的 DocumentSnapshot ,您可以构建 CollectionReference DocumentReference 从中转到新文档.

Since you already have a DocumentSnapshot for the document that you want to create a subcollection under, you can build the CollectionReference and DocumentReference to the new document from that.

类似的事情应该起作用:

Something like this should work:

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

这篇关于如何在Firestore上的文档内部添加集合?反应本机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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