yelp api请求返回“未定义”
我正在对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在这里正确处理诺言。但是,您不会从函数中返回任何内容,然后当您主机记录其输出时,您应该不确定。
尝试以下操作:
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: