0

I am trying to delete parent and child objects where the child 'order' with field orderShared shares 'orders' unique id.

This is my order list

order: [
orderid: 'id1',
orderShared: 'idx1;
name: 'smth']

orders: [
ordersid: 'idx1',
name: 'smthelse']

this is how I am calling function in my pinia store and it throws me an error: Invalid document reference. Document references must have an even number of segments, but users/SXhPv4hvRUVPVN3mbBdjxSEffwB2/clients/lJpA7a33yBUZ3vQBsa9T/orders/sMuSSuTq1vJDlYLqKNfe/order has 7

ordersId in my method is unique ID of 'orders'

It seems that I am doing something wrong when I use where() and get() but I can't figure out what exactly is wrong.

    deleteBatchOrders(ordersId) {
      const storeAuth = useStoreAuth();
      const batch = writeBatch(db)
      const ordersDelete = this.getOrdersName(ordersId)
      // const orderDelete = this.getOrderName(ordersId)

      let orderRef = doc(db, 'users', storeAuth.user.id, 'clients', ordersDelete.clientShared, 'orders', ordersId, 'order')
      let thisRef = orderRef.where('orderShared', '==', ordersId).get()
      thisRef.forEach(doc => {
        batch.delete(doc.ref)})

      const ordersRef = doc(db, 'users', storeAuth.user.id, 'clients', ordersDelete.clientShared, 'orders', ordersId)
      batch.delete(ordersRef)

      batch.commit()

      return alert('Orders deleted')        
    },
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
BeEmil
  • 61
  • 6

1 Answers1

1

The where() method is a method of the CollectionReference class (or of the Query class). So orderRef should not be defined as a DocumentReference but as a CollectionReference (and BTW it is what the error indicates: you use the wrong path, which is a Collection path since it has an odd number of path elements).

So, if I correctly understand your data model (I'm not 100% sure about what is a doc and what is a subcollection), the following should do the trick:

deleteBatchOrders(ordersId) {
  const storeAuth = useStoreAuth();
  const batch = writeBatch(db)
  const ordersDelete = this.getOrdersName(ordersId)
  // const orderDelete = this.getOrderName(ordersId)

  const orderDocRef = doc(db, 'users', storeAuth.user.id, 'clients', ordersDelete.clientShared, 'orders', ordersId)
  const orderCollRef = collection(orderDocRef, 'order')  // Shouldn't it be named orders, since it seems to be a collection?
  let orderCollSnapshot = orderCollRef.where('orderShared', '==', ordersId).get()
  orderCollSnapshot.forEach(doc => {
    batch.delete(doc.ref)
  })

  batch.delete(ordersRef)

  batch.commit()

  return alert('Orders deleted')        
},
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • it throws me an error TypeError: ordersCollRef.where is not a function. I believe something wrong is of how I use `where` maybe I am messing up Web version 9 with web version 8. – BeEmil Oct 12 '22 at 16:42
  • The above example demonstrates `web version 8`. you can check this [Document](https://firebase.google.com/docs/firestore/query-data/queries#web-version-8_1) for clarification. – Roopa M Oct 13 '22 at 09:28