Node.js 获取api并将其呈现到Dom中
我在HTML中呈现数据时遇到问题,我在控制台中获取对象时没有任何错误,但DOM中没有显示任何内容。您可以在下面的代码中看到Node.js 获取api并将其呈现到Dom中,node.js,function,api,Node.js,Function,Api,我在HTML中呈现数据时遇到问题,我在控制台中获取对象时没有任何错误,但DOM中没有显示任何内容。您可以在下面的代码中看到 fetch("api/meals") .then(res => { return res.json(); }) .then(response => { console.log(response); let root = ` <h2>meals API</h2> `; Array.prototyp
fetch("api/meals")
.then(res => {
return res.json();
})
.then(response => {
console.log(response);
let root = `
<h2>meals API</h2>
`;
Array.prototype.forEach.call(response, array => {
const {
id,
title,
description,
location,
when,
max_reservation,
price,
created_date
} = array;
root += `
<h5>meal ID: ${id}</h5>
<ul class="w3-ul">
<li>title : ${title}</li>
<li>description : ${description}</li>
<li>location : ${location}</li>
<li>when : ${when}</li>
<li>max_reservation : ${max_reservation}</li>
<li>price : ${price}</li>
<li>created_date : ${created_date}</li>
</ul>
</div>
`;
document.getElementById("root").innerHTML = root;
});
});
fetch(“api/膳食”)
。然后(res=>{
返回res.json();
})
。然后(响应=>{
控制台日志(响应);
设根=`
膳食API
`;
Array.prototype.forEach.call(响应,数组=>{
常数{
身份证件
标题
描述
位置,
什么时候
马克斯尤酒店,
价格,
创建日期
}=数组;
根+=`
膳食ID:${ID}
- 标题:${title}
- 描述:${description}
- 位置:${location}
- 时间:${when}
- 最大保留:${最大保留}
- 价格:${price}
- 创建日期:${created_date}
`;
document.getElementById(“root”).innerHTML=root;
});
});
你能在根目录中记录什么吗?另外,您也可以直接在数组上调用forEach,如[1,2,3]。forEach((item)=>{console.log(item);})检查控制台中的document.getElementById(“root”)
return元素,这可能是您的问题,因为我假设您没有错误。感谢您的帮助,我刚刚编辑了上面的问题并添加了控制台的打印屏幕。请看一看,然后告诉我该怎么做。@Hemant,从中我必须输入console.log来找出根目录中的内容吗?谢谢,就在这一行document.getElementById(“根”)之前。innerHTML=root;顺便说一句,我刚刚在响应对象中使用虚拟数据运行了您的代码。如果有id为“root”的元素,代码应该给出预期的结果。