简体   繁体   English

Node.js无法调用未定义的方法“ on”进行响应

[英]Node.js Cannot call method 'on' of undefined for response

The get method was working until I added the extra code: 在添加额外的代码之前,get方法一直有效:

var io = require('socket.io');
var express = require('express');
var app = express();
var fs = require('fs');

var http = require("http"),
    faroo_api = 'hidden',
    url = "http://www.faroo.com/api?q=knicks&start=1&length=10&l=en&src=news&f=json&key=";


var request = http.get(url + faroo_api, function (err, response) {    
    var buffer = "", 
        data;

    if(err){
        console.log("there was an error");
    }
    response.on("data", function (chunk) {
        buffer += chunk;
    }); 

    response.on("end", function (err) {
        data = JSON.parse(buffer);
        console.log(data);
    }); 
}); 

var serv = http.createServer(function(request, res){
    fs.readFile(__dirname + '/text.html', function(err, data){
        if(err){
            res.writeHead(500, {'Content-Type': 'text/plain'});
            return res.end('Error');
        }
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(data);
    });
});
serv.listen(8000);

var serv_io = io.listen(serv);
serv_io.on('connection', function(socket){
    socket.emit('messages', data);
});

now its giving this error message. 现在,它给出此错误消息。 I'm not sure what's going wrong. 我不知道怎么了。 I'm guessing the response is being blocked by something but i'm not sure. 我猜测响应被某些内容阻止了,但我不确定。

there was an error
/Users/AmmarKarim/Desktop/Apps/Projectapp/app.js:21
    response.on("data", function (chunk) {
             ^
TypeError: Cannot call method 'on' of undefined
    at ClientRequest.<anonymous> (/Users/AmmarKarim/Desktop/Apps/Projectapp/app.js:21:14)
    at ClientRequest.g (events.js:180:16)
    at ClientRequest.emit (events.js:95:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1693:21)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
    at Socket.socketOnData [as ondata] (http.js:1588:20)
    at TCP.onread (net.js:528:27)

let me know if you need the html. 让我知道您是否需要html。

function (err, response)

This is the wrong callback. 这是错误的回调。 The callback to http.get (and http.request) is simply function (response) http.get(和http.request)的回调只是function (response)

Please refer this demo: 请参考此演示:

http.get(options[, callback])# Since most requests are GET requests without bodies, Node provides this convenience method. http.get(options [,callback])#由于大多数请求都是不带主体的GET请求,因此Node提供了这种便捷方法。 The only difference between this method and http.request() is that it sets the method to GET and calls req.end() automatically. 此方法与http.request()之间的唯一区别是,它将方法设置为GET并自动调用req.end()。

Example: 例:

http.get("http://www.google.com/index.html", function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法调用未定义的node.js的方法“ each” - TypeError: Cannot call method 'each' of undefined node.js 无法调用未定义的node.js的方法“ forEach” - cannot call method 'forEach' of undefined node.js node.js自定义事件:无法调用未定义的方法“ emit” - node.js custom events: Cannot call method 'emit' of undefined Node.js:无法调用未定义的方法版本(MySql) - Node.js: Cannot call method release of undefined (MySql) Node.js TypeError:无法调用未定义的方法“写入” - Node.js TypeError: Cannot call method 'write' of undefined Node js Application无法调用未定义的方法 - Node js Application cannot call method of undefined 通过node.js命令行界面报告创建azure hdinsight“无法调用未定义的方法&#39;filter&#39;” - Create azure hdinsight by node.js command line interface report “Cannot call method 'filter' of undefined” 类型错误:在 node.js 中聚合 mongo 时无法调用未定义的方法“toArray” - TypeError: Cannot call method 'toArray' of undefined while aggregatein mongo in node.js Node.js-猫鼬路径验证失败-TypeError:无法调用未定义的方法“ validate” - Node.js - Mongoose path validation failing - TypeError: Cannot call method 'validate' of undefined socket.io无法调用未定义的方法“ emit”(使用node.js和express,jade) - socket.io Cannot call method 'emit' of undefined(with node.js and express, jade)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM