Falcor Router should return the value from external API -
i new javascript frameworks , trying setup falcor router calling external api (for consider express api app + mango db, hosted @ 3000 port).
now, able use request package (commented out lines) , call express api app (which returns obj.rating = 4). unable send value falcor router instead of hard-coded value "5".
below falcor-router's server.js code:
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 code 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 tried @ global variable declaration, synchronize http request calls, promises, statements etc. nothing seemed work, missing out here - not sure what.
the router's get handler expects return value promise or observable resolves pathvalue. request against db work, return promise resolves pathvalue, e.g.
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 }; }); } } ]);
Comments
Post a Comment