0

I am new to Node.js and I am stuck with small issue. My api is returning JSon but I am not able to see JSon in data or response.

router.get('/search/:id', function(req, res){client.get("http://localhost:3000/api/search/5600678e1c76b4680e0d6544", function(data, response){
         
        console.log(data);
        console.log(response);

res.render('test', {test: data,  user : req.user , title : 'Home'});
    });
});
Zoe
  • 27,060
  • 21
  • 118
  • 148
Rajesh
  • 380
  • 5
  • 13

2 Answers2

1

EDIT: I understood you wrong here another way of how to do it using the npm module request

var request = require('request');

router.get('/search/:id', function(req, res) {
  request('http://localhost:3000/api/search/5600678e1c76b4680e0d6544', function(error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body)
      var data = body;
      res.render('test', {
        test: data,
        user: req.user,
        title: 'Home'
      });
    } else {
      res.end('Error: ' + error);
    }
  });
});

If this is not working than maybe the error is on localhost:3000 not returning anything.

Alesfatalis
  • 769
  • 2
  • 13
  • 32
0

When you call this :

console.log(data);

"data" is a json formated object.

Try to use this :

console.log(JSON.parse(data));

To see a json object.