为什么出现此hgetall类型错误?

时间:2019-02-06 16:09:38

标签: redis node-redis

这是有问题的代码:

let key = "player:"+player_id;
console.log(key);
console.log(typeof(key));

redisClient.hgetall(key, function (err, result) {

if (err) { 
console.log(err);
res.end(false);
}

console.log("this is the reply");
console.log(result);
res.end(result);
});

这是console.log的输出和错误:

player:5db138fdc0
string
this is the reply
{ name: 'Noah' }
_http_outgoing.js:739
      throw new TypeError('First argument must be a string or Buffer');
      ^

TypeError: First argument must be a string or Buffer
    at ServerResponse.end (_http_outgoing.js:739:13)
    at Command.callback (/home/noah/http_service/index.js:228:21)
    at normal_reply (/home/noah/http_service/node_modules/redis/index.js:726:21)
    at RedisClient.return_reply (/home/noah/http_service/node_modules/redis/index.js:824:9)
    at JavascriptRedisParser.returnReply (/home/noah/http_service/node_modules/redis/index.js:192:18)
    at JavascriptRedisParser.execute (/home/noah/http_service/node_modules/redis-parser/lib/parser.js:574:12)
    at Socket.<anonymous> (/home/noah/http_service/node_modules/redis/index.js:274:27)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)

typeof调用显示它是一个字符串,因此我真的很想知道下一步应该做什么。

1 个答案:

答案 0 :(得分:0)

好的,结果不是字符串格式。因此,我在res.end()中使用了JSON.stringify()。我应该一直在结果而不是键上使用typeof()调用。

相关问题