gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:17:53 24 4
gpt4 key购买 nike

我知道 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 函数并将一个函数作为参数传递,然后在 getCoupons 函数的末尾我调用参数函数作为回调,同时传递在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
})
}

遗憾的是,这不会调用回调函数,或者至少不会等待 getCoupons 函数中的第二次 http 调用完成,然后返回到我在输出中列出的模型...

有人有什么想法吗?

最佳答案

在 bixby 中编写 JavaScript 函数有点不同,因为一切都是同步运行的。避免使用依赖于 promise 或回调的代码,因为它们可能无法正常工作。

这是文档中的示例函数,说明了 HTTP GET。尝试修改它以使用您的代码。

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;
}

关于javascript - Bixby,单个 Javascript 函数中的多个 HTTP 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com