Vue.js - push method for object throws Type Error
I am using push() method to add data to an object and receiving an Error:
Uncaught (in promise) TypeError: this.message.push is not a function
Some data is received from an API call, which is to be added to the object.
var price = new Vue({
delimiters: ["[[","]]"],
el: '#app',
data: {
message: {}
},
mounted () {
{% for i in arr %}
axios
.get('https:apicall/symbol={{ x }}')
.then(response => (this.message.push({name : response.data.price , cost: response.data.price.regularMarketPrice.fmt}))
{% endfor %}
}
})
However when I changed:
message: []
and
.then(response => (this.message.push(response.data.price))
it worked fine.
I am using Vue in Django framework and am new to Vue.js
push
is an array method not an object method, an array could be initialized as message:[]
and an object like message:{}
, so you could push a data to an array or assign that data to an object or a property inside that object like :
this.message=response.data.price
or
this.message.price=response.data.price