-1
Good evening, folks. Could you help me with a question.
I have set up an ajax so that I can update the table asynchronously, but I cannot access the ajax return Object. my code:
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
$.ajax({
dataType: "json",
type: 'GET',
url: "/api/vendedor",
}).done(function( data ) {
var atualizaTable = '';
$.each(data, function(key, value) {
console.log(value);
atualizaTable += '<tr>';
atualizaTable += '<td>'+ value.nome +'</td>';
atualizaTable += '<td>'+ value.rg +'</td>';
atualizaTable += '<td>'+ value.email +'</td>';
atualizaTable += '<td>'+ value.totalVendas +'</td>';
atualizaTable += '</tr>'
});
$('#atualizaTable').append(atualizaTable);
});
});
My question is to access the data that is within the function $.each(data, function(key, value)
it’s returning me this way in console.log(value)
[{…}]
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {_id: "5ebdf58ef56ac5794a5bb047", nome: "Victor", rg: 19278080, email: "[email protected]", totalVendas: 10, …}
1: {_id: "5ec3295b944349133c2100c7", nome: "JULIANO", rg: 10152020, email: "[email protected]", totalVendas: 5, …}
2: {_id: "5ec32975944349133c2100c8", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
3: {_id: "5ec3297a944349133c2100c9", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
4: {_id: "5ec3299d944349133c2100ca", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
5: {_id: "5ec32a76944349133c2100cb", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
What I need to do to access this data if I step one value.nome
he returns me undefined
I keep waiting, obigado.
There’s an index [0] there. Try
value[0].nome
.– Sam
Simm, there is. But I have more than one name in this indece if I pass value[0]. name it accesses a guy only
– victor
And if I access as quoted it returns me: Uncaught Typeerror: Cannot read Property 'name' of Undefined
– victor
As it is returning the data accessing only the Indice value[0]: {_id: "5ebdf58ef56ac5794a5bb047", name: "Victor", rg: 19278080, email: "[email protected]", totalVendas: 10, ...}
– victor
returns Undefined =(
– victor