Node.js:Unable to invoke pipe function

2.4k views Asked by At

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

2

There are 2 answers

0
lwang135 On BEST ANSWER

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);
   }
2
alandarev On

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.