I want to send JSON from an API request done on the server to the client side so I can put that information into a Google map.
The request is working. I am getting a JSON object and I can print that to the console but if I try to use the object as a parameter or save it to a variable and print that it returns 'undefined'.
Are either of these things possible? This is where the server runs the request:
router.get("/location/:latitude/:longitude", function(req, res) {
yelp(req.params.latitude, req.params.longitude);
res.redirect("/find");
});
And this is my function:
var request_yelp = function(latitude, longitude) {
var yelp = new Yelp({
consumer_key: consumer_key,
consumer_secret: consumer_secret,
token: token,
token_secret: token_secret,
});
yelp.search({
term: 'food',
ll: latitude + ',' + longitude
})
.then(function (data) {
/* Convert data to JSON string */
var str = JSON.stringify(data);
/* Parse JSON string to JSON Object */
var obj = JSON.parse(str).businesses;
return obj;
}) .catch(function (err) {
console.error(err);
});
I've tried doing something like this when the server runs the code:
var json = yelp(req.params.latitude, req.params.longitude);
Ive also tried creating a global variable and assigning it to that before calling it out side the function. And tried returning the 'data', 'str' and 'obj' variables from the function. A few other things also but I always get the same 'undefined' response.
The function I was trying to send the object to is just the Google Map's createMarker function which is run on the client side. I haven't got much code to show for that as I haven't been able to retrieve the JSON to begin using it.