使用node.js从Firestore中的子集合中删除文档 [英] Delete a document from subcollection in Firestore with node.js
问题描述
我想从Firebase子集合中删除文档.我正在尝试通过以下方式做到这一点:
I'd like to remove a document from Firebase sub-collection. I'm trying to do this in the following way:
firestore.collection('categories').doc(categoryId).collection('books').doc(bookId).delete();
它不起作用.
但是,我可以从集合中删除文档:
However, I'm able to remove a document from the collection:
firestore.collection('categories').doc(categoryId).delete();
我看不见东西了吗?应该如何运作?
Am I losing sight of something? How should it work?
已更新:
const firebase = require('../firebase/firebaseAdmin');
const firestore = firebase.firestore();
module.exports = {
removeBookFromCategory: (categoryId, bookId) => (
firestore
.collection('categories')
.doc(categoryId)
.collection('books')
.doc(bookId)
.delete()
),
};
我在这里有正确的ID,但出现500错误:
I have correct ids here but I'm getting 500 error:
错误:参数"documentPath"不是有效的ResourcePath.路径必须是一个非空字符串.
Error: Argument "documentPath" is not a valid ResourcePath. Path must be a non-empty string.
推荐答案
当您删除具有关联子集合的文档时,不会删除这些子集合.仍然可以通过引用访问它们.执行时,
When you delete a document that has associated subcollections, the subcollections are not deleted. They are still accessible by reference. When you execute,
firestore.collection('categories').doc(categoryId).collection('books').doc(bookId).delete();
它将删除该文档.但是它不会删除子集合.如果要删除子集合,则必须手动完成.请在此处
it will delete the document. But it won't delete subcollections. If you want to delete subcollections it has to be done manually. Please refer here
这篇关于使用node.js从Firestore中的子集合中删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!