【问题标题】:Redis queue stores items in list as [object Object]Redis 队列将列表中的项目存储为 [object Object]
【发布时间】:2014-07-03 12:43:24
【问题描述】:

我正在使用 curl 向我的 node.js 应用程序发送 POST 请求。然后处理 POST 请求并将其推送到 Redis 队列。 但是当我检查我的redis队列时,列表中的所有项目都显示为[object Object]。 这是我的代码,

var http = require('http');
var qs = require('querystring');
var redis =  require('redis').createClient();

redis.lpush('XMLqueue','post');

console.log('Server Starting Now: ');

http.createServer(function (req, res) {

    //res.writeHead(200, {'Content-Type': 'text/plain'});
    //res.end('Hello World\n');

    if(req.method == 'POST'){

        var body="";

        req.on('data', function(data){
            body += data;
        });

        req.on('end',function(){
            var post = qs.parse(body);
            console.log(post);

            //var p = post.toString();
            var p = JSON.encode(post);
            redis.lpush('XMLqueue',p);
        });
    }
});

只有我谨慎推送的“帖子”才能正确显示。 否则所有项目都显示为 1)“[对象对象]” 2)“[对象对象]” . . 我使用这个 curl 命令来传递数据,

curl --data "What=Hello&world=name" http://127.0.0.1:8124

我用这个命令使用另一个终端检查redis队列,

>lrange XMLqueue 0 -1

我该如何解决?

【问题讨论】:

  • 尝试var p = JSON.stringify(post) 而不是var p = JSON.encode(post)。我过去也遇到过类似的问题。

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


【解决方案1】:

Redis 只接受少数输入结构。 所以我们需要将 JSON 转换为字符串。所以我们使用

var p=JSON.stringify(post)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2020-09-29
    • 2020-06-23
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多