与Falcor一起使用请求促销时出错



我正在尝试使用request-promise(RP)软件包进行猎鹰呼叫外部API。我在" RES"(第8行)中获得响应,但我无法将其返回到Falcor模型路径(第13行)。它给出了"未被告求(在承诺)" 错误。

另外,我尝试将返回语句(第13行)放入当时的块(即)之后。)" 错误。

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

让我知道这里缺少什么。

尝试从 rp()调用:

返回承诺
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
        })
    }
  }])
}))

您可以使用Async/等待这样的等待:

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
      }
    })
  }])
}))

相关内容

最新更新