[英]Error while using Request-Promise with Falcor
I am trying to do a Falcor GET call to an external api using Request-Promise (rp) package. 我正在尝试使用Request-Promise(rp)包对外部api进行Falcor GET调用。 I am getting the response in "res" (line no.8) but I am not able to return it to the Falcor model path (line no.13).
我在“ res”(第8行)中得到响应,但无法将其返回到Falcor模型路径(第13行)。 It gives a "Uncaught (in promise)" error.
它给出了一个“未捕获(承诺)”错误。
Also, I tried to put the return statement (line 13) inside the then block (ie) after line 8. Then it give "GET http://localhost/getBusinessTypes ... 500 (Internal Server Error)" error. 另外,我尝试将return语句(第13行)放在第8行之后的then块(即)内。然后给出“ GET http:// localhost / getBusinessTypes ... 500(内部服务器错误)”错误。
1) router.get('/getBusinessTypes.json', falcorServer.dataSourceRoute(function (req, res) {
2) return new falcorRouter([
3) {
4) route: "businessTypes.all",
5) get: function() {
6) rp('http://localhost:8000/service?method=getBusinessTypes')
7) .then(function (res) {
8) console.log("Response from external Api: " + res);
9) })
10) .catch(function (err) {
11) console.log(err);
12) });
13) return {path: ["businessTypes", "all"], value: $atom(res)};
14) }
15) }
16) ]);
17) }));
Let me know what is missing here. 让我知道这里缺少什么。
Try returning the promise from the rp() call: 尝试从rp()调用返回promise:
router.get('/getBusinessTypes.json', falcorServer.dataSourceRoute(function (req, res) {
return new falcorRouter([{
route: "businessTypes.all",
get: function() {
return rp('http://localhost:8000/service?method=getBusinessTypes')
.then(function (res) {
console.log("Response from external Api: " + res)
return {
path: ["businessTypes", "all"],
value: $atom(res)
}
})
.catch(function (err) {
console.log(err)
// Handle error
})
}
}])
}))
You can use async/await like this: 您可以像这样使用async / await:
router.get('/getBusinessTypes.json', falcorServer.dataSourceRoute(function (req, res) {
return new falcorRouter([{
route: "businessTypes.all",
get: async function() {
try {
let result = await rp('http://localhost:8000/service?method=getBusinessTypes')
console.log("Response from external Api: " + result)
return {
path: ["businessTypes", "all"],
value: $atom(result)
}
}
catch(err) {
console.log(err)
// Handle error
}
})
}])
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.