簡體   English   中英

無法通過sidecar讓falcor路由器使用外部API引用數據

[英]Having trouble getting falcor router to reference data using an external API through sidecar

為了測試功能,我已經設置了一些Netflix OSS微服務(Eureka,Falcor邊車)以及非常基本的Falcor設置。 我正在嘗試從Falcor的客戶端調用一個外部API,該API的端點為http://localhost:8001/customer/customers 在我的index.js文件中,我有:

app.use('/model.json', falcorExpress.dataSourceRoute(function (req, res) {
return new falcorRouter([
    {
        route: "customers.length",
        get: function (pathset) {
            return http.get("http://localhost:8001/customer/customers")
                .then(function (json) {
                    return {
                        path: pathset,
                        value: $atom(json.length)
                    };
                });
        }
    }
   ]);
}));

然后在我的客戶端index.html

<html>
 <head>
  <script src="js/falcor.browser.js"></script>
  <script>
    var model = new falcor.Model({source: new falcor.HttpDataSource('/model.json') });
    model.
    get("customers.length").
    then(function(response) {
        console.log(response);
    });
  </script>
 </head>
<body>
</body>
</html>

如果我手動點擊http://localhost:8001/customer/customers API,我將返回一個JSON對象,如下所示:

[
  {
    "customerId": 1,
    "customerName": "Pierre"
  },
  {
    "customerId": 2,
    "customerName": "Paula"
  }
]

但是,我的console.log輸出的是這樣的錯誤對象:

[{path: ["customers","length"],
  value: {message: "undefined is not a function"}}]

在這個階段,我對返回哪種格式的對象不太感興趣,我只想玩點什么。 如何修改路由器以獲取期望的數據?

謝謝

你應該試試這個...

服務器

    // index.js
var falcorExpress = require('falcor-express');
var Router = require('falcor-router');

var express = require('express');
var app = express();
var fs = require("fs");
var q = require("q");

app.use('/model.json', falcorExpress.dataSourceRoute(function(req, res) {
    var jdata;

    function getJson() {
        var defer = q.defer();
        fs.readFile(__dirname + "/" + "abc.json", 'utf8', function(err, data) {
            // console.log(data);
            if (err) {
                defer.reject(err);
            } else {
                jdata = JSON.parse(data);
                defer.res
                defer.resolve(jdata);
            }
        });
        return defer.promise;
    }

    return new Router([
        {
            // match a request for the key "falcor.greeting"    
            route: "falcor.greeting",
            get: function() {
                return { path: ["falcor", "greeting"], value: "hello" };
            }
        },
        {
            // match a request for the key "fa.greeting"
            route: 'fa.greeting',
            get: function() {
                return getJson().then(function(data) {
                    console.log(data.fa.greeting);
                    return { path: ["fa", "greeting"], value: data.fa.greeting };
                }, function(error) {
                    console.log(error);
                    return error;
                });
            }
        },
        {
            // match a request for the key "sa.ga"
            route: 'sa.ga',
            get: function() {
                return getJson().then(function(data) {
                    console.log(data.sa.ga);
                    return { path: ["sa", "ga"], value: data.sa.ga };
                }, function(error) {
                    console.log(error);
                    return error;
                });
            }
        }
    ]);
}));

// serve static files from current directory
app.use(express.static(__dirname + '/'));

var server = app.listen(8008, function(err) {
    if (err) {
        console.error(err);
        return;
    }
    console.log("navigate to http://localhost:8008");
});

客戶

<!-- index.html -->
<html>

<head>
    <!-- Do _not_  rely on this URL in production. Use only during development.  -->
    <script src="//netflix.github.io/falcor/build/falcor.browser.js"></script>
    <script>
        var model = new falcor.Model({
            source: new falcor.HttpDataSource('/model.json')
        });

        //logging:
        var log = console.log.bind(console);
        var jlog = function(x) {
            console.log(JSON.stringify(x, null, 3));
        };
        var jerror = function(x) {
            console.error(JSON.stringify(x, null, 3));
        };


        model.get("falcor.greeting").then(jlog, jerror)

        model.get("fa.greeting").then(jlog, jerror)

        model.get("sa.ga").then(jlog, jerror)
    </script>
</head>

<body>
</body>

</html>

JSON文件

{
    "fa": {
        "greeting": "Hello World"
    },
    "sa": {
        "ga": "ha"
    }
}

瀏覽器控制台上的輸出

{
   "json": {
      "falcor": {
         "greeting": "hello"
      }
   }
}
(index):15 {
   "json": {
      "fa": {
         "greeting": "Hello World"
      }
   }
}
(index):15 {
   "json": {
      "sa": {
         "ga": "ha"
      }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM