Node.js:无法调用管道功能

时间:2014-12-02 08:51:32

标签: node.js apigee usergrid apigee127

我正在尝试一个简单的程序来调用对后端的调用&得到回复,这是我的node.js代码:

'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);
 };

但是我收到以下错误:

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
    ....

我缺少一些我需要下载的npm包吗?

-S

2 个答案:

答案 0 :(得分:3)

你正在打破范围界定。您的功能需要request,但您还加载了var request = require('request')

将其更改为:

   var request = require('request');

   function summary(req, res) {
      request.get(url).pipe(res);
   }

答案 1 :(得分:0)

TypeError: Cannot call method 'pipe' of undefined表示request.get(url)返回undefined。是的,你无法调用undefined对象上的任何函数。

那么,为什么request.get导致未定义?

您可以修改代码以包含一些调试吗?

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

  

我缺少一些我需要下载的npm包吗?

https://www.npmjs.org/package/request列出了16个依赖项,但它们应该自动下载。