【问题标题】:Using fetch command resulted in 'URLSearchParams' in self not defined使用 fetch 命令导致 self 中的“URLSearchParams”未定义
【发布时间】:2020-06-10 18:01:33
【问题描述】:

我写了一个javascript程序:

const fetch = require('whatwg-fetch');
const requestEndpoint = 'https://reqres.in/api/users?page=2';
const Yelp = {
searchYelp(){
   var myInit = {
        method: 'GET'
}
return fetch(requestEndpoint, myInit).then(response => {
  return response.json();}).then(jsonResponse =>{
      console.log(jsonResponse)}).catch(err => 
         console.log(`Error: ${err}`));
}}; 

编辑时,'URLSearchParams' in self not defined 的错误结果,为什么?

请帮忙!

【问题讨论】:

  • 嘿@Mendel,欢迎来到StackOverflow,有几个规则: 1. 请指定更多信息,例如您正在使用的包的版本和脚本的环境。 2. 请确保正确格式化您的代码 - 如果人们能够阅读相关代码,他们可能会帮助您。 3. 在发布问题之前尝试在网络上多搜索一点,here 是指向可能与您的问题相关的 Github 问题的链接。
  • 我使用的whatwg-fetch版本是3.0.0.,这是我能下载的最新版本。我应该在服务器环境中工作。

标签: node.js fetch-api


【解决方案1】:

您需要使用whatwg-fetch 模块吗?

您可以尝试使用node-fetch 模块。

您遇到的错误似乎没有任何充分的理由,它发生在您加载模块时。这个模块是为在 node.js 中使用而设计的吗?

你可以试试:

const fetch = require('node-fetch');
const requestEndpoint = 'https://reqres.in/api/users?page=2';

const Yelp = {
    searchYelp() {
        var myInit = {
                method: 'GET'
        }
        return fetch(requestEndpoint, myInit).then(response => {
            return response.json();
        }).then(jsonResponse => {
                console.log(jsonResponse)
        }).catch(err => 
                console.log(`Error: ${err}`)
        );
    }
};

Yelp.searchYelp();

【讨论】:

  • 是的,whatwg-fetch 用于浏览器。
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 2019-01-19
  • 2021-05-08
  • 1970-01-01
  • 2017-02-04
  • 2018-05-29
相关资源
最近更新 更多