嗨,我对javascript很陌生,正在尝试使用我制作的api并遇到错误,这里是错误:
Uncaught (in promise) ReferenceError: att is not defined
at script.js:24
at Array.forEach (<anonymous>)
at script.js:10
这是我的JavaScript:
function getData(){
fetch('http://localhost:8080/api/read')
.then((res)=> {
return res.json();
})
.then((data)=> {
let result = `<h2> User Info From sampleUser.json </h2>`;
data.forEach((booking) => {
const { bookingId, bookingDesc, bookedUser, bookedUserName, bookedEmail,
bookingCode, dateBooked, startTime, endTime, att: {attendeesEmail} } = booking
result +=
`<div>
<h5> User ID: ${bookingId} </h5>
<ul class="w3-ul">
<li> Booking Description : ${bookingDesc}</li>
<li> User: ${bookedUser} </li>
<li> Username : ${bookedUserName}</li>
<li> User Email: ${bookedEmail} </li>
<li> Date Booked: ${dateBooked} </li>
<li> Start time : ${startTime}</li>
<li> End time: ${endTime} </li>
<li> Attendees Email: ${att[0]["attendeesEmail"]} </li>
</ul>
</div>`;
document.getElementById('result').innerHTML = result;
});
})
}
答案 0 :(得分:0)
将att: {attendeesEmail}
更改为att: attendeesEmail
以使其正确解析。这称为destructuring assignment,通过匹配键名来完成。