pat310 / google-trends-api

An API layer on top of google trends
https://www.npmjs.com/package/google-trends-api
MIT License
896 stars 178 forks source link

Usage with sails.js #63

Closed theunreal closed 7 years ago

theunreal commented 7 years ago

I'm trying to use this api with sails.js, but the response return the trends response data. The controller function looks like this:

googleTrends.interestOverTime({keyword: [req.param('keyword')]})
        .then(function(results){
            return res.send(results);
        })
        .catch(function(err){
          return res.json(err);
        });

Returns this:

image

Is there any way to return the data as json object?

pat310 commented 7 years ago

Hmm, have you tried JSON.parse on the string? If it's valid JSON that should work.

On May 18, 2017, at 12:04, Eliran Elnasi notifications@github.com wrote:

I'm trying to use this api with sails.js, but the response return the response. The controller function looks like this:

googleTrends.interestOverTime({keyword: [req.param('keyword')]}) .then(function(results){ return res.send(results); }) .catch(function(err){ return res.json(err); }); Returns this:

Is there any way to return the data as json object?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

theunreal commented 7 years ago

@pat310, when trying to JSON.parse the data received using the above code, an error occurs:

Uncaught SyntaxError: Unexpected token o in JSON at position 1

pat310 commented 7 years ago

Ohh it looks like there is a return in the first character causing the error. Until I get a fix up, you could JSON.parse(results.slice(1)) (or you might need to .slice(2)). Does that work for you?

theunreal commented 7 years ago

@pat310 JSON.parse(data.data.slice(1)) does the trick :)

Dayjo commented 7 years ago

Wondering if we should just pass a real JSON object here rather than a string. That way we can validate everything and throw an error before this could possibly happen.

pat310 commented 7 years ago

@Dayjo I think that's a good idea since all the methods essentially return a JSON object (in <v4 I don't think that was the case, although I can't remember anymore). Want to make a PR? :smile:

pat310 commented 7 years ago

Closing this for now. I think for now we should continue to just pass a string and have the user modify it as they see fit. If we start slicing characters from the string and validating that it is proper JSON, it will be harder to maintain (whenever the api changes we would need to check that it hasn't changed) and if we say that it is not valid JSON, the user will get nothing back when in reality there may be data available in the string that is formatted slightly differently than we were expecting.