问题描述
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
如何将 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上的文档内部添加集合?反应本机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!