2

I am working on an infinite scroll solution and I am trying to push data onto an array of objects, but I am getting an error:

vue.common.js?e881:2643 [Vue warn]: Error when rendering anonymous component at /Users/Deric/Sites/mobile/lawn-mobile/src/components/jobs/index.vue: warn @ vue.common.js?e881:2643Vue._render @ vue.common.js?e881:2253(anonymous function) @ vue.common.js?e881:1699get @ vue.common.js?e881:736run @ vue.common.js?e881:805flushSchedulerQueue @ vue.common.js?e881:623nextTickHandler @ vue.common.js?e881:401
vue.common.js?e881:2262 TypeError: Cannot read property 'length' of undefined(…)

If i just set the data, it works, but the infinite scroll overwrites the data instead of appending to it. Here is my data object:

data () {
  return {
    jobs: [],

and here is my http call:

getJobs(pageNumber = 1) {
  let api = SessionStorage.get.item('api')
  let url = `${api.base_url}jobs?page=${pageNumber}`
  this.$http.get(url).then((response) => {
    console.log(response.body.data)
    this.jobs.push(response.body.data)

The console.log looks like this:

enter image description here

What is the trick to getting this to work? I have also tried concat but that doesn't work either.

1
  • My guess is to check if this refers to right object Commented Nov 27, 2016 at 15:22

2 Answers 2

2

Are you using concat correctly, i.e.

this.jobs = this.jobs.concat(response.body.data)

only if the output of your console.log is correct this should give you an array of objects which seems to be what you are after.

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

4 Comments

I bet this is the reason. I was not assigning it to my jobs variable but instead using it more like then push method. I'll test as soon as I get home.
That was the trick! I was doing this: this.jobs.concat(response.body.data) think it would "push" the data instead of reassigning the variable. Thanks!
hello, can you explain to me why the concat works but not the push I don't understand
@Snoxik so long as this is referencing the reactive component then this.jobs.push() will work as expected - I don't use this.$http anymore but with axios and async/await the above example should work with either method.
0

It looks like this is not pointing to correct object, try following-

getJobs(pageNumber = 1) {
  let api = SessionStorage.get.item('api')
  let url = `${api.base_url}jobs?page=${pageNumber}`
  let self = this 
  this.$http.get(url).then((response) => {
    console.log(self.jobs)
    console.log(response.body.data)
    self.jobs.push(response.body.data)

Comments

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.