Problems loading a list of Json in Vue.js

Asked

Viewed 613 times

1

note the Javascript code below;

var app = new Vue({
    el:'#app',
    data:{
        bancodedados:[]

    },
    methods:{

    },
    created:function(){
        var self = this;
            self.$http.get('https://swapi.co/api/planets/1/?format=json').then(function(response){
            /*console.log(response); */
            self.bancodedados = response.data;  
        });

    }


});

This is my page;

<table class="table">
        <thead>
            <tr>
                <th>nome</th>
                <th>Title</th>
                <th>Nome</th>
            </tr>   
        </thead>    
        <tbody>
            <tr v-for="bancodedado in bancodedados">
                <td>{{ bancodedado.name }}</td>
                <td>{{ bancodedado.climate }}</td>
                <td></td>
            </tr>
        </tbody>
    </table>

What is happening is that he is able to load all the list but I can’t visualize as shown below;

inserir a descrição da imagem aqui

Maybe the problem is in html code, just need to know how to fix;

====================================================================

see what appears on my consoles;

gave this result with this command;

console.log(response); 

inserir a descrição da imagem aqui

That means he can load the Json records!

but if it’s with that command;

consoles.log(this.bancodedados);

is quite different;

inserir a descrição da imagem aqui

  • what appears if Voce gives a console.log(this.bancodeados) ?

  • I just modified my post, could you take a look please.

  • bancodedados must have a json of the response, by the console print, json is not in Response, but in Response.body. Try to put it that way: self.bancodedados = response.data.body; and see for sure

  • It would actually be self.bancodedados = response.body;. But this json is not a list/array as you expect, it is a single object, with data from only one planet.

2 answers

1


This url that you have only returns one object. Use another to return an array to use in bancodedados. This other url has an array in the property results.

Example:

new Vue({
  el: '#app',
  data: {
    bancodedados: []

  },
  methods: {

  },
  created: function() {
    var self = this;
    self.$http.get('https://swapi.co/api/planets/?format=json').then(function(response) {
      self.bancodedados = response.body.results;
    });

  }


});
table {
  width: 100%;
  display: table;
  table-layout: fixed;
}

table td,
table th {
  display: table-cell;
  text-transform: capitalize;
  text-align: center;
  padding: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<div id="app">
  <table class="table">
    <thead>
      <tr>
        <th>Nome</th>
        <th>Title</th>
        <th>Diametro</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="bancodedado in bancodedados">
        <td>{{ bancodedado.name }}</td>
        <td>{{ bancodedado.climate }}</td>
        <td>{{ bancodedado.diameter }} Km</td>
      </tr>
    </tbody>
  </table>
</div>

  • 1

    It worked, thank you very much.

  • 1

    By the incredible that it seems I am following video lessons of a web institution, and I realize that you have corrected several mistakes of this course that I am doing, that ugly thing of this institution, teaching with video with wrong codes. : P

  • 1

    @wladyband may be to encourage students to discover bugs? : ) But if there are mistakes and the level of the students is not yet enough to correct them then it is a problem.

0

You’re using it wrong, you’re retrieving the answer like this ... response.data, when it’s actually response.body

Just change that line self.bancodedados = response.response.body;

Jsbin

  • still didn’t take.

  • 1

    edited the answer, try now

  • the design is very small, you want me to put it on github to take a look?

  • 1

    You better, because if it doesn’t get hard to help

  • with this new change no house success, I still need help.

  • I’ll put it on github, just a minute, I just wanted to make a remark, it’s a java project, but if you take it and remove the main page and the javascript and css files you’ll be able to get outside the java project.

  • https://github.com/wladyband/vue/tree/master/vue/src/main/resources

  • 1

    I’ve updated the Jsbin link to understand what’s going on, the question is the format of your json’s return

  • would have to test another Json links?

  • if that’s what it is, you could give me the link you tested?

  • @wladyband I updated my response with the link to Voce test

Show 6 more comments

Browser other questions tagged

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