Node.js Redis lpush错误

时间:2014-07-07 10:04:41

标签: javascript node.js express redis node-redis

我正在使用node.js和redis创建一个Web应用程序。 我想将每个传入的请求推送到redis队列,然后再将其推送到数据库。 但是每次程序到达lpush命令时都会出错。 这是我的index.js代码,

var express = require('express');
var app=express();
var http = require('http').Server(app);
var io=require('socket.io')(http);
var qs=require('querystring');
var redis=require('redis');
var client = redis.createClient();
var count=0;
var parseString = require('xml2js').parseString;
//var countm=1;
//var counte=1;

app.get('/adminm', function(req, res){
  res.sendfile('adminmovies.html');
});

app.get('/admine', function(req, res){
  res.sendfile('adminevents.html');
});

app.post('/', function(req,res){
    count=count+1;
    var body="";
    req.on('data',function(data){
    body+=data;
    });
    req.on('end',function(){
    console.log(body);
    var xml=body;
    var parsed="";
    parseString(xml, function(err,result){
        console.dir(result);
        parsed=result;
    });
    //var post=qs.parse(body);
    io.emit('count1',count);
    redis.lpush('events',parsed);
    //console.log(post);

    });


});

/*io.on('connection',function(socket){

});*/


http.listen(3000, function(){
  console.log('listening on *:3000');
});

错误是,

TypeError: undefined is not a function
    at IncomingMessage.<anonymous> (/home/kevin/Desktop/express-app/index.js:37:8)
    at IncomingMessage.emit (events.js:104:17)
    at _stream_readable.js:902:16
    at process._tickCallback (node.js:343:11)

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您正在创建redis客户端,但未在任何地方使用它。

在第37行,您直接在导入的模块上调用lpush

 redis.lpush('events',parsed);

其中lpush未定义。