Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Mostrar la imagen de favico de la base de datos a html usando javascript muestra "indefinido"

He leído muchos ejemplos similares aquí en SO todavía, no puedo entender cuál es el problema. La imagen item.picture se muestra correctamente en el html y su URL también se puede imprimir en la consola.

Sin embargo, el favico muestra undefined tanto en la consola como en html.
Aquí está el código de ejemplo:

 $(function () { jQuery.ajax({ type:'GET', url:"api/wedding/?format=json&limit=20", dataType:"json", success :function (data) { var listA=$("#wedding_id"); for(var i=0;i<data.results.length;i++) { var item=newDiv(data.results[i]) listA.append(item); } }, error:function (e) { console.log(e) // Better for debugging // alert("Error" + e) } }); }); function newDiv(item) { if (item.pic) { var wed_pic = item.pic.split(",")[0]; } if (item.favico) { var favicon = item.favico.split("/")[0]; } console.log(item.pic); // <--- can display the pic_url on console console.log(favicon); //shows 'undefined' var template = '<ul class="wedding-list" id='+ item.wedding_id +'>' + '<a class="wedding-icon" href="#">' + favicon + '</a>' //<--- where I think lies the problem + '<a class="wed_img" target="_blank" href=' + item.url + '>' + '<img src=' + wed_pic + '>' + '</a> </ul>' + return template }

¿Cuál podría ser la causa del error undefined ? Gracias por ayudar.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

para comenzar

 const listA = document.querySelector('#wedding_id') ; fetch ('api/wedding/?format=json&limit') .then (res => res.json()) .then (data => { for (let row of data.results) listA.appendChild( newDiv(row)) }) .catch (err => console.error(err)) ; function newDiv(item) { let e_UL = document.createElement('ul') , wed_pic = !!item?.pic ? item.pic.split(',')[0] : '' , favicon = !!item?.favico ? item.favico.split('/')[0] : '' ; console.log('item ----->', JSON.stringify(item,0,2) ) console.log('wed_pic ->', wed_pic ) console.log('favicon ->', favicon ) e_UL.id = item.wedding_id e_UL.className = 'wedding-list' e_UL.innerHTML = ` <a class="wedding-icon" href="#"> ${favicon} </a> <a class="wed_img" target="_blank" href="${item.url}"> <img src="${wed_pic}"> </a>` return e_UL }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!