I'm playing with docker
and redis
and I want to start redis from the official docker image:
docker run -p 6379:6379 redis
1:C 02 Oct 2022 08:11:14.410 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 02 Oct 2022 08:11:14.410 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 02 Oct 2022 08:11:14.410 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 02 Oct 2022 08:11:14.410 * monotonic clock: POSIX clock_gettime
1:M 02 Oct 2022 08:11:14.411 * Running mode=standalone, port=6379.
1:M 02 Oct 2022 08:11:14.411 # Server initialized
1:M 02 Oct 2022 08:11:14.412 * Ready to accept connections
In my nodejs application I try to connect to the container but it failed and I got this error:
➜ red node app
connect!!! Promise { <pending> }
/Users/user/myapp/red/node_modules/@redis/client/dist/lib/client/RESP2/encoder.js:17
throw new TypeError('Invalid argument type');
^
TypeError: Invalid argument type
at encodeCommand (/Users/user/myapp/red/node_modules/@redis/client/dist/lib/client/RESP2/encoder.js:17:19)
at RedisCommandsQueue.getCommandToSend (/Users/user/myapp/red/node_modules/@redis/client/dist/lib/client/commands-queue.js:187:45)
at Commander._RedisClient_tick (/Users/user/myapp/red/node_modules/@redis/client/dist/lib/client/index.js:440:76)
at Commander._RedisClient_sendCommand (/Users/user/myapp/red/node_modules/@redis/client/dist/lib/client/index.js:424:82)
at Commander.commandsExecutor (/Users/user/myapp/red/node_modules/@redis/client/dist/lib/client/index.js:170:154)
at Commander.BaseClass.<computed> [as set] (/Users/user/myapp/red/node_modules/@redis/client/dist/lib/commander.js:8:29)
at /Users/user/myapp/red/app.js:17:10
at processTicksAndRejections (node:internal/process/task_queues:96:5)
This is my code:
(async () => {
const redis = require("redis");
const client = redis.createClient({
socket: {
host: "localhost",
port: 6379,
},
// password: '<password>'
});
client.on("error", (err) => {
console.log("Error " err);
});
await client.connect();
client.set("bla", true);
const y = client.get("bla");
console.log("connect!!!", y);
})();
Why I can't able to connect to redis container?
CodePudding user response:
You can only set strings or numbers:
client.set("bla", true);
should be
client.set("bla", "true");