【问题标题】:Why can't I resolve Omdb promise? [closed]为什么我不能解决 Omdb 承诺? [关闭]
【发布时间】:2019-11-18 13:43:11
【问题描述】:

我正在学习进行 api 调用。所以我开始使用 OMDB api 来获取电影信息。

fetch(http: //www.omdbapi.com/?apikey=f69f0628&s=batman )
    .then((success) => {
      success.json()
    })
    .then((movies) => {
      console.log(movies)
    })
    .catch((err) => {
      console.log(err)
    });

上面的代码给了我firefox中的错误

SyntaxError: missing ) after argument list

当我这样做时(在 url 中添加单引号):

fetch('http://www.omdbapi.com/?apikey=f69f0628&s=batman')
  .then((success) => {
    success.json()
  })
  .then((movies) => {
    console.log(movies)
  })
  .catch((err) => {
    console.log(err)
  });

它给出了控制台日志:

undefined

【问题讨论】:

  • .then((success) =>{ success.json() } ) 应该是 .then((success) =>{ return success.json() } ).then((success) => success.json() ) - 请参阅箭头函数 documentation 以了解
  • fetch API 要求您将资源作为字符串或请求对象传递,否则为什么要这样做

标签: javascript es6-promise fetch-api omdbapi


【解决方案1】:

问题出在.then((success) =>{ success.json() } )

应该是.then((success) => success.json() ) 没有{ }

.then((success) =>{ return success.json() } )

你可以找到箭头函数的详细信息here

fetch('http://www.omdbapi.com/?apikey=f69f0628&s=batman')
  .then((success) => success.json())
  .then((movies) => {
    console.log(movies)
  })
  .catch((err) => {
    console.log(err)
  });

【讨论】:

    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2019-01-25
    相关资源
    最近更新 更多