Bixby,一个Javascript函数中的多个HTTP调用?
我知道bixby developer studio是全新的,但我在一个javascript函数中进行两个http调用时遇到了一个问题,第一个调用从服务获取自定义标识符,第二个调用基于该标识符从服务获取数据 我试过以下几件事:Bixby,一个Javascript函数中的多个HTTP调用?,javascript,bixby,Javascript,Bixby,我知道bixby developer studio是全新的,但我在一个javascript函数中进行两个http调用时遇到了一个问题,第一个调用从服务获取自定义标识符,第二个调用基于该标识符从服务获取数据 我试过以下几件事: module.exports.function = function(phoneNumber,couponBrand) { if(phoneLookup(phoneNumber)) { return getCoupons(couponBr
module.exports.function = function(phoneNumber,couponBrand)
{
if(phoneLookup(phoneNumber))
{
return getCoupons(couponBrand)
}
else
{
return null
}
}
它不调用任何一个函数。。。
然后我尝试调用第一个函数作为前提条件,如下所示:
module.exports = {
function:getCoupons,
preconditions:[phoneLookup]
}
它不调用函数,只调用前提条件函数。。。
然后,我还尝试了一个非常nodeJS的回调方案,在phoneLookup函数中,我调用了getCouples函数并将一个函数作为参数传递,然后在getCouples函数的末尾,我将参数函数作为回调调用,同时传递在phoneLookup函数中获得的值,如下所示:
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
})
}
遗憾的是,这不会调用回调函数,或者至少不会等到getCouples函数中的第二个http调用完成后再返回到我在输出中列出的模型
有人有什么想法吗?在bixby中编写JavaScript函数有点不同,因为一切都是同步运行的。避免使用依赖于承诺或回调的代码,因为它可能不起作用 下面是文档中的一个示例函数,演示了一个HTTPGET。尝试修改它以使用您的代码
我认为,你可以简单地按顺序调用两个函数 如下图所示
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.xxurl,函数将等待响应。
Bixby的javascript代码按顺序运行。它不支持异步调用和多线程您的函数不返回任何内容,它们使用回调来传播结果。这就是为什么这个
if(phoneLookup(phoneNumber))
不起作用,而这个return-getCoupons(couponBrand)
不起作用。签出或wich是建立在承诺之上的。你对这一行动的模式是什么?phoneNumber
和couponBrand
概念的模型是什么?phoneNumber和couponBrand是简单的概念:integer(phoneNumber){description(与客户帐户关联的电话号码)}
text(couponBrand){description(优惠券的品牌)}
是的,我已经看到了,但是如果我需要在一个函数中执行两个请求,会发生什么呢?然后你发出这两个请求并返回你的操作函数应该返回的任何内容。它是线性代码:var resp1=http.getUrl(…);var resp2=http.getUrl(…)。。。返回代码>
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)
}