Skip to content
Advertisement

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))

Advertisement

Answer

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))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement