Node.js and Express - Sending JSON object from SoundCloud API to the front-end makes it a string

acmutus

I have been using an http.get() to make calls to the SounbdCloud API method to receive a JSON object that I would like to pass to the browser. I can confirm that the data I receive is an object, since I the typeof() method I call on the data prints out that it is an object.

var getTracks = http.get("http://api.soundcloud.com/tracks.json?q="+query+"&client_id=CLIENT_ID", function(tracks) {
    tracks.on('data', function (chunk) {
        console.log(typeof(chunk)); // where I determine that I receive an object
        res.send(chunk);
    });
    //console.log(tracks.data);
}).on("error", function(e){
    console.log("Got error: "+e);
});

But when I check the data I receive in the AJAX request I make in the browser, I find that the data received has a type of String (again, I know this by calling typeof())

$('#search').click(function(e){
  e.preventDefault();  
  var q = $("#query").val();
  $.ajax({ 
        url: '/search',
        type: 'POST', 
        data: { 
          "query": q
        }, 
        success: function(data){
          alert(typeof(data));
          alert(data);
        }, 
        error: function(xhr, textStatus, err){
          alert(err);
        }
      })
  });    

I would appreciate the help, since I do not know where the problem is, or whether I am looking for the answer in the wrong places (perhaps it has something to do with my usage of SoundCloud's HTTP API)

TaLha Khan

JSON is a string. I assume you need an Object representing your JSON string.
Simply use the following method.

var obj = JSON.parse(data);


Another example would be:

var jsonStr = '{"name":"joe","age":"22","isRobot":"false"}';
var jsonObj = JSON.parse(jsonStr);
jsonObj.name //joe
jsonObj.age // 22 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Express, Node, Angular sending audio file to front end

From Dev

Creating dynamic link by requesting data from front-end in Node.js and Express.js

From Dev

Express not sending object from Service to API controller

From Dev

How to Sending Json object through node js server to express-handlebars html?

From Dev

Sending Data to the front end in Express and accessing it

From Dev

Pass data from Vue front end to Node/Express backend

From Dev

Express session not persisting between api calls from react front end

From Dev

Send Object as string, not JSON, via Node Express

From Dev

Sending data from Node/Mongoose to Backbone; only receiving JSON object

From Dev

Displaying a single array item from JSON in PHP (NODE.JS and EXPRESS API)

From Dev

How to pass variable from back-end to front-end in node.js ejs template

From Dev

How to pass variable from back-end to front-end in node.js ejs template

From Dev

Linking back-end (MongoDB via Mongoose, on Express server in Node.js) to front-end (Bootstrap or Foundation) in a reactive way?

From Dev

Node.js/Express POST data not sending

From Dev

Using http query string as a database object node.js/express

From Dev

Sending Javascript from node express to an angular service

From Dev

Sending error as JSON to the front-end using HttpServletResponse

From Dev

After sending response, how to end the current request processing in Node/Express?

From Dev

Returned JSON properties are not string in my node.js express app

From Dev

Sending JSON Object from android

From Dev

Sending multiple responses(res.json) with the same response object in Express.js

From Dev

Sending POST form data from AngularJS client to Express/Node.js server

From Dev

Sending JSON data from Nodejs Express to Backbone

From Dev

Send data back to node.js server from front-end

From Dev

keep a node js function running without using any calls from front end

From Dev

Importing a json file from a url using node js (express)

From Dev

Node.js(express) .end() with JSONP

From Dev

How create a json object from POST data in Express.js?

From Dev

JSON data posts from Angular 2 front end to Asp.Net Core web api - loses values

Related Related

  1. 1

    Express, Node, Angular sending audio file to front end

  2. 2

    Creating dynamic link by requesting data from front-end in Node.js and Express.js

  3. 3

    Express not sending object from Service to API controller

  4. 4

    How to Sending Json object through node js server to express-handlebars html?

  5. 5

    Sending Data to the front end in Express and accessing it

  6. 6

    Pass data from Vue front end to Node/Express backend

  7. 7

    Express session not persisting between api calls from react front end

  8. 8

    Send Object as string, not JSON, via Node Express

  9. 9

    Sending data from Node/Mongoose to Backbone; only receiving JSON object

  10. 10

    Displaying a single array item from JSON in PHP (NODE.JS and EXPRESS API)

  11. 11

    How to pass variable from back-end to front-end in node.js ejs template

  12. 12

    How to pass variable from back-end to front-end in node.js ejs template

  13. 13

    Linking back-end (MongoDB via Mongoose, on Express server in Node.js) to front-end (Bootstrap or Foundation) in a reactive way?

  14. 14

    Node.js/Express POST data not sending

  15. 15

    Using http query string as a database object node.js/express

  16. 16

    Sending Javascript from node express to an angular service

  17. 17

    Sending error as JSON to the front-end using HttpServletResponse

  18. 18

    After sending response, how to end the current request processing in Node/Express?

  19. 19

    Returned JSON properties are not string in my node.js express app

  20. 20

    Sending JSON Object from android

  21. 21

    Sending multiple responses(res.json) with the same response object in Express.js

  22. 22

    Sending POST form data from AngularJS client to Express/Node.js server

  23. 23

    Sending JSON data from Nodejs Express to Backbone

  24. 24

    Send data back to node.js server from front-end

  25. 25

    keep a node js function running without using any calls from front end

  26. 26

    Importing a json file from a url using node js (express)

  27. 27

    Node.js(express) .end() with JSONP

  28. 28

    How create a json object from POST data in Express.js?

  29. 29

    JSON data posts from Angular 2 front end to Asp.Net Core web api - loses values

HotTag

Archive