0

I wrote an javascript programme:

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}`));
}}; 

On edit, an error result in 'URLSearchParams' in self not defined, why?

Please help!

2
  • 1
    Hey @Mendel, welcome to StackOverflow, a couple of rules: 1. Please specify more information such as the version of the packages you're using and the environment of your script. 2. Please make sure to properly format your code - people are likely to help you if they can read the code in question. 3. Try searching on the web a little bit more before posting questions, here is a link to a Github issue that might be relevant to your problem. Commented Jun 10, 2020 at 10:10
  • The whatwg-fetch version that I used is 3.0.0., which is the latest version that I could download. I should be working in a server environment. Commented Jun 11, 2020 at 6:04

1 Answer 1

1

Do you need to use the whatwg-fetch module?

You could try the node-fetch module instead.

There doesn't seem to be any good reason for the error you're getting, it happens when you load the module. Is this module designed for use within node.js?

You could try:

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, whatwg-fetch is for browsers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.