我无法使用node-redis在redis中设置和获取密钥。但是set和get在使用redis-cli时工作得很好。这是我的代码:
const redis = require('redis');
const util = require('util');
const GLOBAL_KEY = 'lambda-test';
const redisOptions = {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
}
try{
test();
}
catch(err) {
console.log('catch err ' + err);
}
async function test() {
var client = redis.createClient(redisOptions);
client.on("error", function(error) {
console.error(error);
});
client.get = util.promisify(client.get);
client.set = util.promisify(client.set);
await client.set("ke", "test1", redis.print).catch(function(err){console.log('err: '+ err);});
await client.get("ke", redis.print).catch(function(err){console.log('err: '+ err);});
}输出:
err: ReplyError: ERR syntax error
err: ReplyError: ERR wrong number of arguments for 'get' command不知道原因是什么,有没有人能帮我弄清楚我错过了什么?
更新:我尝试使用绑定,但没有成功:
client.get = util.promisify(client.get).bind(client);
client.set = util.promisify(client.set).bind(client);此外,我还启用了调试以获取额外的日志:
2021-04-20T07:48:36.279Z Queueing set for next server connection.
2021-04-20T07:48:36.303Z Stream connected XXXXX:6379 id 0
2021-04-20T07:48:36.303Z Checking server ready state...
2021-04-20T07:48:36.304Z Send XXXXXX:6379 id 0: *1
$4
info
2021-04-20T07:48:36.305Z Net read XXXXX:6379 id 0
2021-04-20T07:48:36.306Z Redis server ready.
2021-04-20T07:48:36.306Z on_ready called XXXXX:6379 id 0
2021-04-20T07:48:36.307Z Sending offline command: set
2021-04-20T07:48:36.307Z Send XXXXX:6379 id 0: *4
$3
set
$2
ke
$5
test1
$187
function print (err, reply) {
if (err) {
// A error always begins with Error:
console.log(err.toString());
} else {
console.log('Reply: ' + reply);
}
}
2021-04-20T07:48:36.307Z Net read XXXXX:6379 id 0
err: ReplyError: ERR syntax error
2021-04-20T07:48:36.309Z Send XXXXX:6379 id 0: *3
$3
get
$2
ke
$187
function print (err, reply) {
if (err) {
// A error always begins with Error:
console.log(err.toString());
} else {
console.log('Reply: ' + reply);
}
}
2021-04-20T07:48:36.310Z Net read XXXXX:6379 id 0
err: ReplyError: ERR wrong number of arguments for 'get' command发布于 2021-04-21 20:48:08
感谢所有人的建议。我把这个问题作为问题发布在repo https://github.com/NodeRedis/node-redis/issues/1599上,并从leibale那里得到了答案。
async function test() {
const client = redis.createClient(redisOptions);
client.on("error", function(error) {
console.error(error);
});
client.get = util.promisify(client.get).bind(client);
client.set = util.promisify(client.set).bind(client);
try {
console.log('SET reply:', await client.set("ke", "test1"));
} catch (err) {
console.error(err);
}
try {
console.log('GET reply:', await client.get("ke"));
} catch (err) {
console.error(err);
}
}问题出在bind和使用redis.print作为回调
https://stackoverflow.com/questions/67174022
复制相似问题