【发布时间】:2019-10-07 08:31:11
【问题描述】:
在我的简单项目中,使用不同的 API 一切正常,但在 this API 中存在问题
输入值为:
帖子 ID:未定义
这是我的代码:
let PostShow =
{
render : async () => {
let request = Utils.parseRequestURL()
let post = await getPost(request.id)
return /*html*/`
<section class="section">
<h1> Post Id : ${post.id}</h1>
</section>
`
}
, after_render: async () => {
}
}
而输出值为:
帖子 ID:未定义
输入和输出都是一样的。我做错了什么?
【问题讨论】:
-
能分享一下getPost方法吗?
-
是的。让 getPost = async (id) => { const options = { method: 'GET', headers: { 'Content-Type': 'application/json' } };尝试 { const response = await fetch(
https://countriesnode.herokuapp.com/v1/countries+id, options) const json = await response.json(); // console.log(json) return json } catch (err) { console.log('Error getting documents', err) } }
标签: javascript ecmascript-6 async-await fetch