admin管理员组文章数量:1335615
I am using a call to my database to retrieve some results and pushing them onto an array. However when I console.log(this.activeBeers)
I don't get an array back but instead an object. How can I get a plain array back instead of a object?
Vueponent('beers', {
template: '#beers-template',
data: function() {
return {
activeBeers: []
}
},
ready: function() {
function getActiveBeers(array, ajax) {
ajax.get('/getbeers/' + $('input#bar-id').val()).then(function (response) {
$.each(response.data, function(key, value) {
array.push(value.id);
});
}, function (response) {
console.log('error getting beers from the pivot table');
});
return array;
}
console.log(this.activeBeers = getActiveBeers(this.activeBeers, this.$http));
},
props: ['beers']
});
I am using a call to my database to retrieve some results and pushing them onto an array. However when I console.log(this.activeBeers)
I don't get an array back but instead an object. How can I get a plain array back instead of a object?
Vue.ponent('beers', {
template: '#beers-template',
data: function() {
return {
activeBeers: []
}
},
ready: function() {
function getActiveBeers(array, ajax) {
ajax.get('/getbeers/' + $('input#bar-id').val()).then(function (response) {
$.each(response.data, function(key, value) {
array.push(value.id);
});
}, function (response) {
console.log('error getting beers from the pivot table');
});
return array;
}
console.log(this.activeBeers = getActiveBeers(this.activeBeers, this.$http));
},
props: ['beers']
});
Share
Improve this question
edited Jan 28, 2016 at 14:58
Mykola
3,3736 gold badges26 silver badges40 bronze badges
asked Jan 28, 2016 at 14:55
Stephan-vStephan-v
20.4k32 gold badges121 silver badges210 bronze badges
3 Answers
Reset to default 2AJAX is done asynchronously so you won't be able to just return the value that you do not have yet.
You should console.log your stuff after the $.each
to see what you received.
As the other answers pointed out, your getActiveBeers()
call is returning before the callback that fills the array gets executed.
The reason your array is an object is because Vue wraps/extends arrays in the underlying data so that it can intercept and react to any mutating methods - like push, pop, sort, etc.
You can log this.activeBeers
at the beginning of your ready
function to see that it's an object.
By the way, if you want to log the unwrapped/plain array of activeBeers
, you can use your ponent's $log
method:
this.$log(this.activeBeers);
The other answer is correct, getActiveBeers
sends an HTTP request and then immediately returns the array, it doesn't wait for the ajax request to e back. You need to handle the updating of activeBeers
in the success function of the ajax request. You can use the .bind()
function to make sure that this
in your success function refers to the Vue
ponent, that way you can just push the ids directly into your activeBeers
array.
Vue.ponent('beers', {
template: '#beers-template',
data: function() {
return {
activeBeers: []
}
},
ready: function() {
this.getActiveBeers();
},
methods: {
getActiveBeers: function(){
this.$http.get('/getbeers/' + $('input#bar-id').val()).then(function (response) {
$.each(response.data, function(key, value) {
this.activeBeers.push(value.id);
}.bind(this));
console.log(this.activeBeers);
}.bind(this), function (response) {
console.log('error getting beers from the pivot table');
});
}
}
props: ['beers']
});
本文标签: javascriptHow do I get a plain array back from vuejs with a componentStack Overflow
版权声明:本文标题:javascript - How do I get a plain array back from vuejs with a component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392214a2466213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论