Javascript 谷歌云功能删除子集合;遗失文件
我有一个名为Posts的Firestore集合,Posts中的每个文档都可以有一个名为Post Likes和/或Post Comments的子集合。当我删除Posts文档时,它不会删除子集合,因此我在Firestore中留下一个缺少文档的引用,如下所示: 我在我的Google Cloud函数中使用以下代码来查找缺少数据的帖子集合中的引用,然后对于缺少引用的每个文档,我想删除帖子喜欢和帖子评论的子集合。现在,我只是尝试列出子集合文档,以便删除它们,但我得到了一个错误Javascript 谷歌云功能删除子集合;遗失文件,javascript,node.js,google-cloud-firestore,google-cloud-functions,firebase-admin,Javascript,Node.js,Google Cloud Firestore,Google Cloud Functions,Firebase Admin,我有一个名为Posts的Firestore集合,Posts中的每个文档都可以有一个名为Post Likes和/或Post Comments的子集合。当我删除Posts文档时,它不会删除子集合,因此我在Firestore中留下一个缺少文档的引用,如下所示: 我在我的Google Cloud函数中使用以下代码来查找缺少数据的帖子集合中的引用,然后对于缺少引用的每个文档,我想删除帖子喜欢和帖子评论的子集合。现在,我只是尝试列出子集合文档,以便删除它们,但我得到了一个错误 function delet
function deleteOrphanPostSubCollections() {
let collectionRef = db.collection('Posts');
return collectionRef.listDocuments().then(documentRefs => {
return db.getAll(...documentRefs);
}).then(documentSnapshots => {
for (let documentSnapshot of documentSnapshots) {
if (documentSnapshot.exists) {
console.log(`Found document with data: ${documentSnapshot.id}`);
} else {
console.log(`Found missing document: ${documentSnapshot.id}`);
return documentSnapshot.getCollections().then(collections => {
return collections.forEach(collection => {
console.log('Found subcollection with id:', collection.id);
});
});
}
}
return
});
}
但是,我得到了以下错误。请帮我解决这个问题
这是因为没有用于存储的
getCollections()
方法
如果要列出与DocumentSnapshot
相对应的文档的所有集合,则需要使用以下方法:
documentSnapshot.ref.listCollections()
.then(collections => {
for (let collection of collections) {
console.log(`Found subcollection with id: ${collection.id}`);
}
});
此外,请注意,如果在循环中调用异步方法,建议使用该方法,以便在解析所有“输入”承诺时返回解析的单个承诺。这是因为没有用于循环的
getCollections()
方法
如果要列出与DocumentSnapshot
相对应的文档的所有集合,则需要使用以下方法:
documentSnapshot.ref.listCollections()
.then(collections => {
for (let collection of collections) {
console.log(`Found subcollection with id: ${collection.id}`);
}
});
此外,请注意,如果在循环中调用异步方法,建议使用该方法以返回一个承诺,该承诺在所有“输入”承诺都已解决时解决。Hey@ramluro,您有时间查看建议的解决方案吗?Hey@ramluro,您有时间查看建议的解决方案吗?