2014-12-02 33 views
0

我想了一個簡單的程序來調用到後端的呼叫&得到的迴應背後,是我的Node.js代碼:的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

回答

3

您正在劃定範圍。你的功能需要request,但你也有加載var request = require('request')

更改它,這樣你有:

var request = require('request'); 

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

TypeError: Cannot call method 'pipe' of undefined意味着request.get(url)undefined返回。是的,你不能在undefined對象上調用任何函數。

那麼,爲什麼request.get導致undefined?

你能修改代碼來包含一些調試嗎?

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表16間的依賴關係,但他們應該自動下載。

+0

@ Iwang135如果我們談論https://github.com/request/request則沒有,只需要一個arguement - * URL * – alandarev 2014-12-02 09:07:53

+0

對不起,我添加了更新。我有些困惑。爲了記錄,它_does_採用多個參數,但在使用'.pipe'時不需要 – lwang135 2014-12-02 09:10:00