Push() does not work in Vuejs?

Asked

Viewed 481 times

4

I need to push() the data of Vuejs and make it add another object in the object array it has there. But when I click on the button I programmed for you to push() it just doesn’t.

Follows code:

atualizar () {
    axios({
      method: 'get',
      url: '/server/inbox'
    }).then(function (response) {
      console.log(response.data)
      for (let e = 0; e < response.data.length; e++) {
        this.emails.push(
          {
            remetente: response.data.remetente,
            destinatario: response.data.destinatario,
            assunto: response.data.assunto,
            texto: response.data.texto
          },
          console.log('PUSH!')
        )
      }
    })
      .catch(function (err) {
        console.log(err)
      })
    console.log('Fim!')
  }

HTML:

<q-list no-border link inset-separator>
      <q-item v-for="email in emails" :key="email.id">
        <q-item-side icon="email"/>
        <q-item-main>
          <q-item-tile :label="emails.remetente">{{ email.remetente }}</q-item-tile>
          <q-item-tile :sublabel="emails.assunto">{{ email.assunto }}</q-item-tile>
        </q-item-main>
      </q-item>
    </q-list>

You’re right that way??

This is the method I created to push the object array, and I’ve already checked the data coming from the server and it’s all right, it’s coming back in the right way.

If anyone can help me I’d appreciate it...

  • Are you sure that this is what you think it is? Test use Arrow functions like this: }).then((response) => {

  • I tried to put Arrow Function, but it gave syntax error, what would be the right place to put it?? Feel free to edit the question code, or add an answer, I’ll be happy to accept the answer if it helps...

  • The best way to thank you is to accept one of the answers as a solution and vote for all that helped you. :)

  • That’s exactly what I did... if I gave it to you I’d take both, but you can only take one, so...

2 answers

3


The function passed to . then da Promise from Axios changes the execution context of that function. You must use Arrow functions, or an alias as Ian suggested.

To use Arrow Function just change

}).then(function(response){

for

}).then((response) => {

Look what you got this.emails.push({...}, console.log('PUSH!')). That one console.log is in the wrong place.

That is, the whole code, fixed:

atualizar() {
  axios({
      method: 'get',
      url: '/server/inbox'
    }).then((response) => {
      console.log(response.data)
      for (let e = 0; e < response.data.length; e++) {
        this.emails.push({
          remetente: response.data.remetente,
          destinatario: response.data.destinatario,
          assunto: response.data.assunto,
          texto: response.data.texto
        });
        console.log('PUSH!', e);
      }
    })
    .catch(function(err) {
      console.log(err)
    })
  console.log('Fim!')
}
  • I put this code there and it returned what comes from the server in the browser console, but still not generating the update on the screen, I will add the HTML in the question and you can tell me what is wrong

  • @Leonardoebert puts a piece of that answer so we can make an example working.

  • @Leonardoebert I believe your problem lies within the scope of this, take a look at my answer calmly.

  • {sender: "Leonardo - <[email protected]>", destinatario: "[email protected]", assunto: "teste2", texto: "Teste2" } assunto: "teste2" destinatario: "[email protected]" sender: "Leonardo " text: "Teste2" I only removed personal data @Sergio This is the answer that comes from the server and appears in the browser console

  • Response comes to the console, but does not enter the object array or update the screen...

  • I’ve always done it that way and it’s always worked, until today...

  • @Leonardoebert is what you’re looking for, right? https://jsfiddle.net/0rykp0o8/

  • That’s exactly it, but it doesn’t work in my code, I put it here: https://codepen.io/LeonardoEbert/pen/GvBNZm Note: I am using the Quasar Framework

  • What gives you console.log(typeof response.data)?

  • Returns object

  • @Leonardoebert ok, and what gives console.log(Array.isArray(response.data), response.data.length);?

  • The latter returns false and undefined

  • @Leonardoebert interesting, this is unexpected. What gives console.log(JSON.stringify(response.data));?

  • Returns a String with the answer coming from the server: {"sender":"Leonardo", "destinatario":"[email protected]", ...}

  • @Leonardoebert then you don’t have an array. Try to take out the loop for and use only this.emails.push(response.data);

  • 1

    At last it worked, thank you very much Sergio, thank you very much...

  • @Leonardoebert of nothing :)

  • 1

    After a lot of fight with the code worked : D... Thank you very much...

Show 13 more comments

2

In that case the this is not referencing $vm. You can do so:

atualizar () {
  var self = this;

  axios({
    method: 'get',
    url: '/server/inbox'
  }).then(function (response) {
    console.log(response.data)
    for (let e = 0; e < response.data.length; e++) {
      self.emails.push(
      {
        remetente: response.data.remetente,
        destinatario: response.data.destinatario,
        assunto: response.data.assunto,
        texto: response.data.texto
      })
    }
  })
  .catch(function (err) {
    console.log(err)
  })
  console.log('Fim!')
}

you can improve your code a little bit, so:

atualizar () {
  var self = this;

  axios.get('/server/inbox')
    .then(({data}) => {
      for (let e = 0; e < data.length; e++) {
        self.emails.push({
          remetente: data.remetente,
          destinatario: data.destinatario,
          assunto: data.assunto,
          texto: data.texto
        })
      }
    })
    .catch((err) => {
      console.log(err)
    })
  console.log('Fim!')
}

You can understand more about this, in this series of videos.

  • Take a look at my suggestion calmly.

  • I will look, incidentally, I liked the series of videos, thank you very much for the recommendation...

  • Did you even look at the HTML?? Maybe the error isn’t there??

  • No, I don’t believe in HTML. You can give a console.log in the mailing list on then?

  • I made a console.log in the self.emails and returned only one array with the two static objects I have already created

  • How you load this array of emails initially?

  • I have 2 objects that I only used to test, I put the code here: https://codepen.io/LeonardoEbert/pen/GvBNZm

  • Try to give a console.log(self.emails) just below that console.log(response.data)

  • I gave one console.log, and it only shows the 2 static objects I have inside the emails[]

  • Thank you so much for your help Ian, thank you so much for

  • @Ianrodrigues you cannot add solution to the question. If you have already answered with the solution below, this is enough. Avoid doing this type of editing.

  • @Articuno, all right, I was just trying to improve what he wrote. I’m still trying to understand the platform. ;)

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.