0

I am trying out a simple program to invoke a call to a backend & get the response back, here is my node.js code:

'use strict';

var util = require('util');
var http = require('http');
var request = require('request');
var stream = require('stream');


module.exports = {
 summary:summary
}

function summary(request, response) {
 var id = request.swagger.params.id.value;
 var url = "http://localhost:8080/test-org/myApp/summaries?q='id'";
 console.log('Executing request: '+url);
 request.get(url).pipe(response);
 };

However I get the following error:

curl http://localhost:10010/v1/customers/1123/summary
Executing request: http://localhost:8080/test-org/myApp/summaries?q='
id'
TypeError: Cannot call method 'pipe' of undefined
    at summary (C:\Apigee127\API-116\api\controllers\summary.js:17:19)
    at swaggerRouter (C:\Apigee127\API-116\node_modules\a127-magic\node_modules\
swagger-tools\middleware\2.0\swagger-router.js:114:18)
    at C:\Apigee127\API-116\node_modules\a127-magic\lib\middleware.js:82:9
    ....

Is there some npm package I am missing that I need to download?

-S

brandonscript
  • 68,675
  • 32
  • 163
  • 220
user2825273
  • 565
  • 1
  • 4
  • 9

2 Answers2

3

You are breaking scoping. You function takes request but you also have loaded var request = require('request').

Change it such that you have:

   var request = require('request');

   function summary(req, res) {
      request.get(url).pipe(res);
   }
lwang135
  • 576
  • 1
  • 3
  • 13
0

TypeError: Cannot call method 'pipe' of undefined means that request.get(url) returns undefined. And yes, you cannnot call any function on undefined object.

So, why request.get results in undefined?

Can you modify the code to include some debugging?

  request.get(url)
  .on('error', function(err) {
    console.log(err)
  })
  .pipe(response);

Is there some npm package I am missing that I need to download?

https://www.npmjs.org/package/request lists 16 dependencies, but they were supposed to auto-download.

alandarev
  • 8,349
  • 2
  • 34
  • 43
  • @Iwang135 if we are talking about https://github.com/request/request then no, it takes only one arguement - *url* – alandarev Dec 02 '14 at 09:07
  • Sorry, I added an update. I got something confused. For the record, it _does_ take multiple arguments, but not necessary when using `.pipe` – lwang135 Dec 02 '14 at 09:10