コレクションからすべてのドキュメントを削除する必要がありますが、「pedido」が「this.pProdutos」と等しい場合にのみ削除する必要があります。問題は、クエリを実行した後でも、コレクション全体のすべてのドキュメントが削除されることです。私は今、以下のコードを使用しています:
this.db.collection('produtosPedidos', ref => ref.where('pedido', '==', this.pProdutos)).ref
.get()
.then(querySnapshot => {
querySnapshot.forEach((doc) => {
doc.ref.delete().then(() => {
console.log("Document successfully deleted!");
}).catch(function(error) {
console.error("Error removing document: ", error);
});
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
この問題は、.ref
ここでのクエリの最後にある末尾が原因で発生します。
ref.where('pedido', '==', this.pProdutos)).ref
最初の部分ref.where('pedido', '==', this.pProdutos))
はクエリを作成しますが、ref
そのクエリを呼び出すCollectionReference
と、コレクション全体にが返されます。末尾.ref
を削除すると、機能するはずです。
this.db.collection('produtosPedidos', ref => ref.where('pedido', '==', this.pProdutos))
.get()
.then(querySnapshot => {
...
このタイプの操作では、AngularFireを介して実行することによる追加の利点はありません。コードを少し減らすために、ベアJavaScriptSDKで実行することをお勧めします。AngularFireはJavaScriptSDKの上に構築されているため、これを行うと2つは完全に相互運用できます。
コード内:
firebase.firestore().collection('produtosPedidos').where('pedido', '==', this.pProdutos)
.get()
.then(querySnapshot => {
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加