Node.js:类型错误:对象不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22593994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js: TypeError: object is not a function
提问by R0b0tn1k
I have a weird error:
我有一个奇怪的错误:
var http = require("http");
var request = require("request");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
});
response.end();
}).listen(8888);
The idea is for it to listen as a webserver, and then do a request. But this is what I get as error:
这个想法是让它作为网络服务器监听,然后发出请求。但这就是我得到的错误:
request('http://www.google.com', function (error, response, body) {
^
TypeError: object is not a function
at Server.<anonymous> (/Users/oplgkim/Desktop/iformtest/j.js:8:2)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2056:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:120:23)
at Socket.socket.ondata (http.js:1946:22)
at TCP.onread (net.js:525:27)
What am I doing wrong? I installed request, so that's not it :)
我究竟做错了什么?我安装了请求,所以不是这样:)
回答by Tim Cooper
Access to the requestglobal variable is lost as you have a local variable with the same name. Renaming either one of the variables will solve this issue:
request由于您有一个同名的局部变量,因此无法访问全局变量。重命名任一变量将解决此问题:
var http = require("http"); var request = require("request");
http.createServer(function(req, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})
response.end();
}).listen(8888);
回答by Sathyapriyan C
You are no longer able to access the global variable 'request'. You need to rename your local variable 'request' with some other name and the problem will be resolved.
您无法再访问全局变量“请求”。您需要使用其他名称重命名本地变量“请求”,问题将得到解决。

