Closed edwardpark closed 9 years ago
You just need to make req.params.id
available the sendJSON
method. I'd do something like getBusInfo.id = req.params.id
.
like this? i'm a little confused how making the getBusInfo.id available to sendJSON connects to variables options ability to populate the url?
var options = {
url: 'https://api.wmata.com/NextBusService.svc/json/jPredictions?StopID=' + getBusInfo.id + '&api_key='+ apiKey,
};
var weather = {
url: 'https://api.forecast.io/forecast/' + darkSkyApiKey + '/' + latitude + ',' + longitude
};
///////////////////////////////////////////////////////////
//issue getting the function call to return the right data.
var getBusInfo = {
busAPIInfo: "",
rez: "",
sendJSON: function(){
this.rez.json(this.busAPIInfo)
}
}
app.get("/busstop/:id", function(req, nodeResponse){
getBusInfo.id = req.params.id;
request(options,function (error, response, body) {
if (!error && response.statusCode == 200) {
getBusInfo.rez = nodeResponse;
getBusInfo.busAPIInfo = JSON.parse(body);
getBusInfo.sendJSON();
}
});//end of request module
});
I'm a little confused too!
If you just want to be able to do stuff with the ID included in the URL, putting getBusInfo.busId = req.params.id
will let you do access this.busId
in your sendJSON
method.
It's just getting the ID from the URL (req.params.id
) and saving that value as a property of the getBusInfo
object.
I'm not sure what you mean by "connects to variables options ability to populate the url".
oh oh! i'm sorry yes so in the options variable we've been using a hardcoded variable call stopID in the url of the api call where you would now see getBusInfo.id which would represent the bus stop id the user would enter in the front end
ie "http://localhost:3000/busStops/110095 " 110095 would be the bus stop id and is what we've been hardcoding into a global variable so far. I would like that "/:id" to instead be in the actual options variable url so that the correct url would be generated for the api call.
var options = {
url: 'https://api.wmata.com/NextBusService.svc/json/jPredictions?StopID=' + [[how to get ":id" path url here?]] + '&api_key='+ apiKey,
};
Ohhh. Make options
into a function!
function options(id){
return {
url: "https://"... + id
}
}
There's not really another way, except for putting options.url = "https://" + req.params.id + etc
in your app.get
oh goodness gracious. thanks Robin!!
no jk.
so our api call is now working but I am trying to connect the url path "/:id" , req.params.id to populate in the variable options but it is not connecting. Is this another issue where I would need a callback to configure this?