简体   繁体   English

Falcor路由器应从外部API返回值

[英]Falcor Router should return the value from external API

I am new to JavaScript frameworks and currently trying to setup a falcor router calling an external api (for now consider it as an express api app + mango db, hosted at 3000 port). 我是JavaScript框架的新手,目前正在尝试设置一个调用外部api的falcor路由器(目前将其视为Express api应用+芒果数据库,托管在3000端口)。

Now, I am able to use the request package (commented out lines) and successfully call the Express Api app (which returns obj.rating = 4). 现在,我可以使用请求包(注释掉的行)并成功调用Express Api应用程序(返回obj.rating = 4)。 But I am unable to send this value from the falcor router instead of the hard-coded value "5". 但是我无法从falcor路由器发送该值,而不是硬编码值“ 5”。

Below is the falcor-router's server.js code: 下面是falcor-router的server.js代码:

app.use('/rating.json', falcorExpress.dataSourceRoute(function (req, res) {
  return new Router([
    {
      route: "rating",
      get: function() {
        var obj;
        // request('http://localhost:3000/rating/101', function (error, response, body) {
        //   obj = JSON.parse(body);
        //   console.log('rating:', obj.rating); // obj.rating = 4
        // });
        return {path:["rating"], value:"5"};
      }
    }
  ]);
}));

The below is the code for index.html : 以下是index.html的代码:

<script>
function showRating() {
  var model = new falcor.Model({source: new falcor.HttpDataSource('http://localhost/rating.json') });
  model.
    get("rating").
    then(function(response) {
      document.getElementById('filmRating').innerText = JSON.stringify(response.json,null, 4);
    });
}
</script>

I also tried to look at the global variable declaration, synchronize http request calls, promises, then statements etc. But nothing seemed to work, clearly I am missing out something here - not sure what. 我还尝试查看全局变量声明,同步http请求调用,promise和语句等。但是似乎没有任何效果,显然我在这里遗漏了一些东西-不知道是什么。

The router's get handler expects the return value to be a promise or an observable that resolves to a pathValue . 路由器的get处理程序期望返回值是可以解析为pathValue的PromiseObservable To get your request against the db to work, simply return a promise that resolves to a pathValue, eg 为了使您对数据库的请求正常工作,只需返回一个解析为pathValue的promise,例如

  return new Router([
    {
      route: "rating",
      get: function() {
        return request('http://localhost:3000/rating/101', function (error, response, body) {
           return { path: ["rating", value: JSON.parse(body).rating };
         });
      }
    }
  ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM