0

Using Vue.

I'm currently stuck at this problem, where it seems whatever data is pushed into my cart: [] is always 1 behind. I'm fairly new to javascript, and I've been spending quite a few days trying to research and try others fixes, but without any kind of luck. - I really hope someone has some input that'll help me out.

Video showing my problem: (https://youtu.be/nNRYSc4BuYo)

-- As seen in the video, my console.log('Cart Spawncode: ' + value.newSpawnCode) is always one behind.

(this.cart is defined earlier in my file as cart: [])

addToCart(category, seller, spawncode, label, description, price, type) {
            if (this.cart == '') {
                console.log('---------------------------------')
                console.log('newSpawnCode = undefined (Added)')
                console.log('Cart Spawncode: ' + this.cart.newSpawnCode) // Print undefined (Good)
                console.log('Spawncode: ' + spawncode)
                console.log('---------------------------------')
                this.cart.push({ newCategory: category, newSeller: seller, newSpawnCode: spawncode, newLabel: label, newDescription: description, newPrice: price, newType: type });
            } else {
                for (const [key, value] of Object.entries(this.cart)) {
                    if (value.newSpawnCode == spawncode) {
                        console.log('---------------------------------')
                        console.log('Exists')
                        console.log('Cart Spawncode: ' + value.newSpawnCode)
                        console.log('Spawncode: ' + spawncode)
                        console.log('---------------------------------')
                    } else {
                        this.cart.push({ newCategory: category, newSeller: seller, newSpawnCode: spawncode, newLabel: label, newDescription: description, newPrice: price, newType: type });
                        console.log('---------------------------------')
                        console.log('Doesent exist')
                        console.log('Cart Spawncode: ' + value.newSpawnCode)
                        console.log('Spawncode: ' + spawncode)
                        console.log('---------------------------------')
                    }
                }
            }
        },
    ```
  • 1
    you probably should not be checking if cart is empty by `this.cart == ''` – Krzysztof Krzeszewski Apr 17 '21 at 09:09
  • I tried with null, undefined, "undefined" and nothing seemed to work but this. - Perhaps you can you'll be able to give me a better way that works? – Deltarix Apr 17 '21 at 09:18
  • well, the proper approach would be to just check the length like so: `this.cart.length === 0` you see, the double comparison sign ignores type of the value, basically in your case both the empty array and empty string are additionaly converted to boolean values, (both falsey). Normally it is advised to use triple comparison sign, so that the code is more predictable [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Krzysztof Krzeszewski Apr 17 '21 at 09:22
  • Thanks a lot! I actually tried using that operator, but the wrong way I see. I will remember that. – Deltarix Apr 17 '21 at 12:10
  • @KrzysztofKrzeszewski An empty array (`[]`) is actually truthy, not falsy. – chipit24 Apr 17 '21 at 22:18
  • you are totally right, it seems it gets converted to a string, which is empty instead – Krzysztof Krzeszewski Apr 18 '21 at 20:00

0 Answers0