0

我正在尝试一个简单的程序来调用后端并获取响应,这是我的 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

4

2 回答 2

3

你正在打破范围。您的功能需要request,但您也已加载var request = require('request').

更改它,以便您拥有:

   var request = require('request');

   function summary(req, res) {
      request.get(url).pipe(res);
   }
于 2014-12-02T09:09:33.127 回答
0

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

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

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

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

是否有一些我需要下载的 npm 包?

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

于 2014-12-02T09:00:22.017 回答