简体   繁体   中英

Yelp API Request returning as "undefined"

I am making an API call to the yelp-fusion . However, when I try to log this result, the log shows as undefined. I assume this is due to something regarding promises or async functions. Note: I need to keep this in a function as I intend to export this function to other files.

const searchRequest = {
  term:'cafe',
  location: 'san francisco, ca'
};

const client = yelp.client(apiKey);

function businessSearch(search){
client.search(search).then(response => {
    return response
  }).catch(e => {
    console.log(e);
  });
}

console.log(businessSearch(searchRequest))

I think you are handling the promise correctly here. However, you are not returning anything from the function, then when you console log its output you should get undefined.

Try this:

function businessSearch(search){
  return client.search(search).then(response => {
    return response
  }).catch(e => {
    console.log(e);
  });
}

console.log(businessSearch(searchRequest))

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