2

I'm trying to build a small application in VueJs, Following is my data set:

data(){
    return {
        pusher: '',
        channel:'',
        notify: [],
        notifications: '',
        notificationsNumber: '',
    }
},

where I'm having an axios call in created property of components as:

axios.get('api/notifications', {headers: getHeader()}).then(response => {
    if(response.status === 200)
    {
        this.notify = response.data.notifications
        this.notificationsNumber = this.notify.length
    }
}).catch(errors => {
    console.log(errors);
})

I'm having pusherJs implemented, so I'm having following code:

this.pusher = new Pusher('xxxxxxxx', {
    cluster: 'ap2',
    encrypted: true
});

var that = this
this.channel = this.pusher.subscribe('stellar_task');
this.channel.bind('company_info', function(data) {
    console.log(data.notification);
    that.notifications = data.notification
});

Once the value is being obtained from pusher I want to push this to my array notify as watch property, something like this:

watch: {
    notifications(newValue) {
        this.notify.push(newValue)
        this.notificationsNumber = this.notificationsNumber + 1
    }
}

So the problem is the data format which I'm receiving through pusher is in object form and push function is not getting implemented in this:

Screenshot:

Console log of pusher response

Help me out with this.

4
  • Can you console.log(this.notify) inside the watch? If it's an array, it must have push.
    – shotor
    Commented Jul 7, 2017 at 14:14
  • @morgh is right, this.notify doesn't look to be an array. Commented Jul 7, 2017 at 14:16
  • @MatWaligora I've already defined it as an array in data set, By the way I don't know what happend but while doing console.log I'm getting the answer. Commented Jul 7, 2017 at 14:22
  • @morgh I did console.log and it is working. Even I removed console.log() it is working now, may be some watch function error during loading the component. Now it is coming perfectly fine. Commented Jul 7, 2017 at 14:24

1 Answer 1

1

I'm making an assumption that response.data.notifications is an Array Like Object.

So all you have to do is:

this.notify = [...response.data.notifications];

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.