yelp api请求返回“未定义”

发布于 2025-01-21 22:23:46 字数 449 浏览 1 评论 0原文

我正在对Yelp-Fusion进行API调用。但是,当我尝试记录此结果时,日志显示为未定义。我认为这是由于有关承诺或异步功能的原因。注意:我需要将其保留在一个函数中,因为我打算将此功能导出到其他文件。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2025-01-28 22:23:46

我认为您在这里正确处理诺言。但是,您不会从函数中返回任何内容,然后当您主机记录其输出时,您应该不确定。

尝试以下操作:

function businessSearch(search){
  return 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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文