0

I have a data method thats setup like this:

data () {
     return {
          live_count: null,
          queue: null,
     }
}

In my mounted method I retrieve data for my queue attribute. The data structure of my queue is there are objects inside it, and each object has an array of objects. (look at screenshot below)

enter image description here

Now after my retrieve is completed I periodically check if some timestamps are greater than 1 hour ago and set either true/false on a new attribute like so:

axios
.get(`/my-queue`)
    .then(response => {
        this.queue = response.data.queue
    })
    .catch(error => {
        console.log(error)
        this.errored = true
    })
    .finally(() => {
        this.loading = false;
        this._timer = setInterval(() => {
            console.log('check queue rows');
            const anHourAgo = Date.now() - (60 * 60 * 1000)
            for (const [lane_id, items] of Object.entries(this.queue)) {
                console.log('lane id: ' + lane_id);
                for(const item of items) {
                    item.past_grace = item.queue_entry_time_ms > anHourAgo
                }
            }
        }, 1000)
    });

In my Vue.js component when I iterate over my queue object I can read/diplay the items from the queue fine, e.g. queue_entry_time_ms, but when I print out past_grace nothing is shown.

Here is what my code looks like:

<template v-for="(lane, lane_id) in queue">
    <template v-for="(item, index) in lane">
        {{ item.queue_entry_time_ms }} <!-- this prints fine -->
        {{ item.past_grace }} <!-- this doesnt -->
    </template>
</template>

In the Vue.js debugger I can see the attribute is set like below:

enter image description here

Is there something I am doing wrong?

3
  • Did you try something like {{ item.past_grace ? "true" : "false" }} Commented Oct 19, 2020 at 22:23
  • When I do this, all iterations print "false" even though their attribute in the object is actually true. Commented Oct 19, 2020 at 22:27
  • What is the structure of this.queue? If you console.log(this.queue) after setting past_grace for each item, does it look as you expect? Commented Oct 19, 2020 at 22:43

1 Answer 1

1

I have experienced this myself. For my issue, the new attribute passed to the object wasn't responsive because Vue wasn't watching for it to change.

My solution was to use Vue.set(), documented here: https://v2.vuejs.org/v2/api/#Vue-set

It allows you to pass a new responsive attribute to an object after the object is first declared in data()

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

7 Comments

Do you know how I can correctly set my attribute? I have tried doing Vue.set(this.queue[lane_id][key], 'past_grace', true); but this doesn't work.
I've also tried this.$set(this.queue[lane_id][key], 'past_grace', true); without any success either.
Have you tried to use set inside your for loop? so - this.$set(item, past_grace, item.queue_entry_time_ms > anHourAgo)
I tried to set the attribute to true first just to see I can see the attribute, I have tried both lines above, and your suggestion. Neither work, in the Vue debugger the attribute isn't showing up against the objects.
Actually, your line did work, it was an issue with my for loop. Thanks!
|

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.