Bixby,单个Javascript函数中的多个HTTP调用



我知道bixby developer studio是全新的,但我在一个javascript函数中进行两个http调用时遇到了问题,第一个调用从服务中获取自定义标识符,第二个调用从基于该标识符的服务中获取数据。

我试过以下几件事:

module.exports.function = function(phoneNumber,couponBrand)
{
if(phoneLookup(phoneNumber))
{
return getCoupons(couponBrand)
}
else
{
return null
}
}

它不调用任何一个函数。。。然后我试着调用第一个函数作为先决条件,如下所示:

module.exports = {
function:getCoupons,
preconditions:[phoneLookup]
}

它不调用函数,只调用前置条件函数。。。然后,我还尝试了一个非常nodeJS的回调方案,在phoneLookup函数内部,我调用了getCoupons函数并将一个函数作为参数传递,然后在getCoupon函数结束时,我调用参数函数作为回调,同时传递phoneLookout函数中获得的值,如下所示:

function getCoupons(json,callback)
{
var endpoint = //removed for security
var body = //removed for brevity
var options = //removed for brevity
var response = http.postUrl(endpoint,body,options)
var json = response.parsed
callback(json)
}
module.exports.function = function phoneLookup(phoneNumber,couponBrand)
{
var endpoint = //removed for security
var body = //removed for brevity
var options = //removed for brevity
var response = http.postUrl(endpoint,body,options)
var json = response.parsed
getCoupons(json,function(results)
{
return results
})
}

遗憾的是,这没有调用回调函数,或者至少没有等待getCoupons函数中的第二个http调用完成,然后再返回到我在输出中列出的模型。。。

有人有什么想法吗?

在bixby中对JavaScript函数进行编码有点不同,因为一切都是同步运行的。避免使用依赖于承诺或回调的代码,因为它可能不起作用。

下面是文档中的一个示例函数,演示了一个HTTPGET。尝试修改它以使用您的代码。

https://github.com/bixbydevelopers/http/blob/master/code/FindShoeFiltering.js

module.exports.function = function findShoe (type) {
console.log("FindShoe filter by a specific type")
var options = { 
format: 'json',
query: {
type: type
}
};
// If type is "Formal", then this makes a GET call to /shoes?type=Formal
var response = http.getUrl(config.get('remote.url') + '/shoes', options);
return response;
}

我认为,您可以简单地按顺序调用2个函数。

如下图所示,

function getCoupons(json,callback)
{
var endpoint = //removed for security
var body = //removed for brevity
var options = //removed for brevity
var response = http.postUrl(endpoint,body,options)
var json = response.parsed
return json
}
module.exports.function = function phoneLookup(phoneNumber,couponBrand)
{
var endpoint = //removed for security
var body = //removed for brevity
var options = //removed for brevity
var response = http.postUrl(endpoint,body,options)
var json = response.parsed
return getCoupons(json)
}

并且,如果您调用http.xxxxUrl,您的函数将等待响应。Bixby的javascript代码按顺序运行。它不支持异步调用和多线程

最新更新