admin管理员组文章数量:1332339
First thing first, the details:
node version: v16
Using redislabs cloud (v6.2.3)
npm package redis version 4.0.3
Here's the code...
const redis = require("redis");
require("dotenv").config();
const client = redis.createClient({
host: process.env.REDIS_URI,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD
});
client.on("connect", () => {
console.log("Connected to our redis instance!");
client.set("iAmAKey", "Value");
});
On running it doesn't output anything :( and just quits simply after some time.
Any help will be appreciated. Thanks!
First thing first, the details:
node version: v16
Using redislabs cloud (v6.2.3)
npm package redis version 4.0.3
Here's the code...
const redis = require("redis");
require("dotenv").config();
const client = redis.createClient({
host: process.env.REDIS_URI,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD
});
client.on("connect", () => {
console.log("Connected to our redis instance!");
client.set("iAmAKey", "Value");
});
On running it doesn't output anything :( and just quits simply after some time.
Any help will be appreciated. Thanks!
Share Improve this question asked Feb 3, 2022 at 14:35 Amresh Prasad SinhaAmresh Prasad Sinha 1361 silver badge7 bronze badges 5-
console.log(process.env)
to see if you manage to get them. – ikhvjs Commented Feb 3, 2022 at 14:41 - Yep it logs the var correctly. – Amresh Prasad Sinha Commented Feb 3, 2022 at 14:57
-
client.on('error', (err) => console.log('Redis Client Error', err));
Any error from this? – ikhvjs Commented Feb 3, 2022 at 15:00 - Nope no error :( The client is not connecting to redis ig as stated by Guy Rose. But the weird thing is its not throwing error. I cant find any working example now of node-redis which works with redis labs :/ – Amresh Prasad Sinha Commented Feb 3, 2022 at 16:46
- This is correct for the behaviour. You can still bind an event to the client even you didn't connect to the redis server and that's why there is no error. – ikhvjs Commented Feb 4, 2022 at 7:40
4 Answers
Reset to default 6Well, I made it work now. Adding this as a reference for any future wanderer with the same issue.
So the munity suggested to use URL instead.
const redis = require("redis");
require("dotenv").config();
(async () => {
const client = redis.createClient({
url: `redis://default:${process.env.REDIS_PASSWORD}@${process.env.REDIS_URI}:${process.env.REDIS_PORT}`,
});
client.on("error", (err) => console.log("Redis Client Error", err));
await client.connect();
await client.set("key", "value");
console.log("Redis Connected!")
const value = await client.get("key");
console.log(value);
})();
Here's the issue for future reference: Not able to connect to redis-labs cloud redis server #1892
The call to .createClient
creates a client but doesn't actually connect to Redis. So the connect event never fires. Node Redis 4.x supports promises so you really don't need the callback at all. You can acplish the same thing with:
const redis = require("redis");
const client = redis.createClient();
await client.connect();
await client.set('foo', 'bar');
let foo = client.get('foo');
Note that I removed the usage of dotenv
and just have it connect to Redis on localhost
in my sample code for brevity.
additionnel information to @Amresh this is what worked for me:
url: `redis://${process.env.REDIS_PASSWORD}@${process.env.REDIS_URI}:${process.env.REDIS_PORT}`,
without default word.
I faced the same issue in Node 14 using Redis ^4.3.1
. Changing the version to ^3.1.2
worked for me.
本文标签: javascriptnot able to connect to redis in nodejsStack Overflow
版权声明:本文标题:javascript - not able to connect to redis in nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742328253a2454160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论