如何从我的承诺结果中访问包含在 [[]](双大括号)内的值?

How do I access to the value enclosed inside [[]] (double large bracket) from my promise result?

我正在处理 Github OAuth 身份验证按钮。单击该按钮将从我的 Github 帐户获取访问令牌,以验证我的应用程序。

为了获得我的 "access token",我必须使用自定义中间件和自定义 "code" 发送请求,GitHub 将交换此代码并向我提供 "access token"(根据Github API).

什么在起作用??
我能够获取代码并将此代码放入我的 "fetch",并且也能够在对象中获取访问令牌。

我获取令牌的代码:

fetch(
    `https://gitfund-oauth.herokuapp.com/authenticate/${CODE}`
  ).then((response) => console.log(response.json()))

这是什么问题?
我收到一个响应,其中访问令牌包含在双大括号内,那么我该如何获得呢? 我收到如下回复:

如何从对象中获取令牌?我尝试执行 response.json().PromiseValue,但没有成功。 如果有人能解释什么是双大括号,我如何访问该值,以及这样的符号在承诺链中意味着什么。

任何帮助都意味着很多。 谢谢

response.json() 也是一个承诺。因此,您应该使用相同的 then/catch promise 或使用 await 运算符。

例如:

fetch(`https://gitfund-oauth.herokuapp.com/authenticate/${CODE}`)
.then((response) => response.json())
.then((json) => console.log(json));

response.json() returns a promise that resolves with the result of parsing the body text as JSON。您将需要在其上链接另一个 .then() 以取回已解决的承诺值,例如:

fetch(`https://gitfund-oauth.herokuapp.com/authenticate/${CODE}`)
  .then((response) => response.json())
  .then((data) => console.log(data))

演示:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(data => console.log(data))

更多信息:Using Fetch

它 returns 另一个承诺,所以我们必须像这样链接它 :

 fetch(`https://gitfund-oauth.herokuapp.com/authenticate/${CODE}`)
    .then((response) => response.json())
    .then((data) => console.log(data.token))
}