Javascript api端点未定义

Javascript api端点未定义,javascript,rest,Javascript,Rest,我正在尝试调用shorturl API,它实现了承诺,但当我将结果记录到console.log中时,它会显示undefined 下面是我的代码 const handleCall = (e) =>{ e.preventDefault(); fetch(`https://api.shrtco.de/v2/shorten?url=www.udemy.com`) .then(res => console.log( res.json())) .then(

我正在尝试调用shorturl API,它实现了承诺,但当我将结果记录到console.log中时,它会显示undefined

下面是我的代码

const handleCall = (e) =>{
    e.preventDefault();
     fetch(`https://api.shrtco.de/v2/shorten?url=www.udemy.com`)
     .then(res =>  console.log( res.json()))
     .then(res => console.log(res))

}

当您将
res.json()包装到
控制台.log中时,您永远不会等待
您的
res.json()
,因为它不再返回承诺。因此,只需删除
控制台.log

const handleCall=(e)=>{
e、 预防默认值();
取回(`https://api.shrtco.de/v2/shorten?url=www.udemy.com`)
.then(res=>res.json())
.then(res=>console.log(res));
}
➔ 产生:
{ok:true,result:{…}}


没有必要记录两次,因为它将是相同的输出。如果确实要在这两个函数中登录,请等待它,然后返回:

const handleCall=(e)=>{
e、 预防默认值();
取回(`https://api.shrtco.de/v2/shorten?url=www.udemy.com`)
。然后(异步res=>{
设x=wait res.json();
控制台日志(x);
返回x;
})
.then(res=>console.log(res));
}

➔ 产生:
{ok:true,result:{…}
{ok:true,result:{…}}

第一个应该返回带有结果的承诺,以便第二个可以使用该链