简体   繁体   中英

API key not defined?

Error as follows:

    /home/runner/CrowdedMajesticRouter/index.js:3
    let apiKey,cJhlPVSd5-wzJ0iOv;

    ^

    SyntaxError: Unexpected token '-'


let apiKey,
cJhlPVSd5-wzJ0iOv;   

let search = (term, location) => {
  return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`, {
    headers:  {
      Authorization: `Bearer ${apiKey}`
    }
  }).then(response => {
    return response.json();
  }).then(jsonResponse => {
    if (jsonResponse.businesses) {
      console.log(jsonResponse.businesses)

    }

  });
}

search('Mexican', 'Los Angeles')  

If the code is corrected as follows:

let apiKey = cJhlPVSd5-wzJ0iOv;

let search = (term, location) => {
  return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`, {
    headers:  {
      Authorization: `Bearer ${apiKey}`
    }
  }).then(response => {
    return response.json();
  }).then(jsonResponse => {
    if (jsonResponse.businesses) {
      console.log(jsonResponse.businesses)

    }

  });
}

search('Mexican', 'Los Angeles')

then the error is:

ReferenceError: cJhlPVSd5 is not defined
    at /home/runner/CrowdedMajesticRouter/index.js:3:14
    at Script.runInContext (vm.js:131:20)
    at Object.<anonymous> (/run_dir/interp.js:156:20)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js

Per yelp: /businesses/search This endpoint returns up to 1000 businesses based on the provided search criteria. It has some basic information about the business. To get detailed information and reviews, please use the Business ID returned here and refer to /businesses/{id} and /businesses/{id}/reviews endpoints.

Note: at this time, the API does not return businesses without any reviews.

From the code you posted we can see you didn't declare your variable.

let apiKey 
somekey; 

It should be

let apiKey, somekey; 

or

let apiKey;
let somekey; 

That's why you're getting the ReferenceError ;

@Edit Now I see

let apiKey,
cJhlPVSd5-wzJ0iOv; 

It should be

let apiKey = 'cJhlPVSd5-wzJ0iOv';   

No idea why you're trying separate it by comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM