【问题标题】:Fetch polyfill, undefined PromiseValue获取 polyfill,未定义的 PromiseValue
【发布时间】:2015-08-16 05:47:08
【问题描述】:

尝试使用 fetch polyfill 发出DELETE 请求,但我得到Uncaught TypeError: Cannot read property 'then' of undefined,这是promise.then() 上的错误

这是我的做法:

function deleteData(item, url) {
  fetch(url + '/' + item, {
    method: 'delete'
  }).then(response => {
    return response.json();
  });
}

另一方面,当我发出/GET 请求时,一切正常:

function fetchData(url) {    
  return fetch(url).then(response =>
    response.json().then(json => {
      return json;
    })
  );
}

知道我做错了什么吗?

【问题讨论】:

  • 我不确定我的回答是否正确,因为你不知道你在哪里得到错误(你 需要 return 但也许你得到另一个之前的错误)。
  • 完美运行。编码太多小时,需要休息一下,再次感谢!

标签: javascript reactjs promise es6-promise


【解决方案1】:

你必须返回承诺:

function deleteData(item, url) {
  return fetch(url + '/' + item, {
    method: 'delete'
  }).then(response => {
    return response.json();
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多