0

For example, when i try console.log(this.products), the console returns a empty array. But the products was populate with a fetch on the created lifecycle. Or when i try to assign value to the variable items.

created() {
        fetch("https://cardapio-digital-api.herokuapp.com/api/v1/products")
            .then(response => {
                return response.json()
            })
            .then(data => {
                this.products = data
                this.productsByCategory = this.products
            })

        console.log(this.products)
        this.items = JSON.parse(localStorage.getItem('items'))
    }
jeanpdt
  • 38
  • 5
  • _"Or when i try to assign value to the variable items."_ not sure what you're saying here – Phil May 18 '20 at 00:54
  • Note that you are not saving the values from `return response.jsonn()` anywhere. Try this: ```.then(response => { this.products = response.json() console.log(this.products) })``` – Vinicius Kammradt May 18 '20 at 00:55
  • @ViniciusKammradt OP is doing it correctly. The code in your comment won't work since `response.json()` returns a promise – Phil May 18 '20 at 01:04

0 Answers0