0

I am trying to do a GET method inside Nodejs. After doing the GET, I want to use the MAP function and collect all the names as an array.

But I am getting the following error,

/root/server.js:21                                                                                                                                                                                       
    var op = body.products.map(function(item) {                                                                                                                                                          
                          ^                                                                                                                                                                              

TypeError: Cannot read property 'map' of undefined                                                                                                                                                       
    at IncomingMessage.<anonymous> (/root/server.js:21:27)                                                                                                                                               
    at emitNone (events.js:85:20)                                                                                                                                                                        
    at IncomingMessage.emit (events.js:179:7)                                                                                                                                                            
    at endReadableNT (_stream_readable.js:913:12)                                                                                                                                                        
    at _combinedTickCallback (internal/process/next_tick.js:74:11)                                                                                                                                       
    at process._tickCallback (internal/process/next_tick.js:98:9)

CODE is,

var http = require('http');
    console.log("hi")
    var options = {
      host: 'api.usergrid.com',
      path: '/siddharth1/sandbox/restaurants'
    };

    var req = http.get(options, function(res) {
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));

      // Buffer the body entirely for processing as a whole.
      var bodyChunks = [];
      res.on('data', function(chunk) {
        // You can process streamed parts here...
        bodyChunks.push(chunk);
      }).on('end', function() {
        var body = Buffer.concat(bodyChunks);
        console.log('BODY: ' + body);
        // ...and/or process the entire body here.
        var op = input.entities.map(function(item) {
           return item.name;
         });
          console.log(op);
      })
    });

    req.on('error', function(e) {
      console.log('ERROR: ' + e.message);
    });

    console.log('Server listening on port 80');

I do not know where I am going wrong, because in console I can see the body having json data.

Kidnly help me out.

John Seen
  • 701
  • 4
  • 15
  • 31
  • 1
    Well, [`Buffer.concat`](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_concat_list_totallength) returns a new `Buffer` instance and `Buffer`s don't have a `products` property. Presumably you have to convert the buffer to a string first and use `JSON.parse` to convert the serialized data into an actual JavaScript value. *"because in console I can see the body having json data."* Right, JSON needs to be **parsed** first. See http://stackoverflow.com/q/45015/218196 . – Felix Kling Jun 27 '16 at 06:12
  • what is the output of console.log('BODY: ' + body); ? – Naga Sai A Jun 27 '16 at 06:13
  • Sorry guys, I am updating the question on map functions, need to change the "products" name. For posting here, I changed my GET URL. – John Seen Jun 27 '16 at 06:14
  • I have updated my question. – John Seen Jun 27 '16 at 06:16
  • @NagaSaiA output of body is, something like this, { "entities": [ { "uuid": "18a33f4a-cc71-11e4-b41f-cf4d22f4b746", "type": "restaurant", "name": "Pomegranate Cafe" }, { "uuid": "18a64c8a-cc71-11e4-ae1e-3f142288e1e6", "type": "restaurant", "name": "Los Taquitos" } ] }; – John Seen Jun 27 '16 at 06:18
  • The error means that the property `product` of `body` is undefined. – Samuel Toh Jun 27 '16 at 06:19
  • if it is input.entities.map, are you getting different error message because before updating post error is at body.products.map – Naga Sai A Jun 27 '16 at 06:23
  • i have used that json and tested in codepen and it looks fine - http://codepen.io/nagasai/pen/dXNLBQ – Naga Sai A Jun 27 '16 at 06:26
  • Hey Naga, thank you for your effort as of now I have a temporary solution. – John Seen Jun 27 '16 at 06:35
  • How can I access the op variable outside the function? – John Seen Jun 27 '16 at 14:53

1 Answers1

0

Thank you, @Felix Kling As you indicated, I added,
var body2 = JSON.parse(body); and i got the array.

  var body2 = JSON.parse(body);
    // ...and/or process the entire body here.
    var op = body2.entities.map(function(item) {
        return item.name;
      });
      console.log(op);

If I have any other query I will get back.

John Seen
  • 701
  • 4
  • 15
  • 31