0

i´m doing a webapp with vuejs and firebase. I got stack using filter an array for 'push' into a data table.

i think i follow correctly de instructions of how to use the filter() method, but is not working

methods: {

 consulProds {
  let resultado = await db.collection('productos')
           .get()                

  resultado.docs.forEach(doc => {
          let productosTot = doc.data()

          let productosInd= productosTot.filter(producto=> 
                            producto.precio== 99)

           this.productosind.push(productosInd)    

  })
 }
} 

when i do the 'push' without the line code of 'filter' method and using 'doc.data()', it works and i get the array in my 'data table'. But does not happend anything using the 'filter'.

3
  • probably because there is no doc.data().precio that equals 99? Commented Sep 25, 2019 at 1:04
  • yes there is. when i do: ' this.productosind.push(doc.data()) ', whith out the line code that is using 'filter', the data came out ok. Commented Sep 25, 2019 at 1:17
  • 2
    can you add some of doc.data() so we can help you further? Commented Sep 25, 2019 at 1:19

1 Answer 1

1

The reason is most probably because productosTot is not an Array but "just" a JavaScript Object (that is not an Array), see https://firebase.google.com/docs/reference/js/firebase.firestore.QueryDocumentSnapshot.html#data

Just check the value of precio with an if, as follows:

methods: {

 consulProds {
  let resultado = await db.collection('productos').get()                

  resultado.docs.forEach(doc => {
     let producto = doc.data()
     if (producto.precio == 99) this.productosind.push(producto)    
  })
 }

} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.