1

While running redis when is try to get data it showing this kind of errors what is wrong with this.

   (node:12362) UnhandledPromiseRejectionWarning: ReferenceError: queueMicrotask is not defined
    at RedisSocket.cork (/home/aathilingam/Desktop/Laravel - Docker/rediscache/node_modules/@node-redis/client/dist/lib/client/socket.js:86:13)
    at Commander._RedisClient_tick (/home/aathilingam/Desktop/Laravel - Docker/rediscache/node_modules/@node-redis/client/dist/lib/client/index.js:410:60)
    at RedisSocket.socket_1.default.on.on.on.on (/home/aathilingam/Desktop/Laravel - Docker/rediscache/node_modules/@node-redis/client/dist/lib/client/index.js:331:86)
    at RedisSocket.emit (events.js:198:13)
    at RedisSocket._RedisSocket_connect (/home/aathilingam/Desktop/Laravel - Docker/rediscache/node_modules/@node-redis/client/dist/lib/client/socket.js:136:10)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:12362) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12362) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My code is for getting details of photos from a site as a json and when i try to load them into redis i got the above error. My code is

const express = require("express");
const axios = require("axios");
const cors = require("cors");
const Redis = require("redis");

//const redisClient = Redis.createClient();
const DEFAULT_EXPIRATION = 3600

const app = express();
app.use(express.urlencoded({ extended: true}));
app.use(cors());


let redisClient;

(async () => {
  redisClient = Redis.createClient();

  redisClient.on('error', (err) => console.log('Redis Client Error', err));

  await redisClient.connect();
  
})();

app.get("/photos",async(req,res) => {
    const albumId = req.query.albumId;
    // redisClient.get(`photos?albumId=${albumId}`,async (error, photos) => {
    //     if(error) console.error(error)
    //     if(photos != null){
    //         console.log("cache hit");
    //         return res.json(JSON.parse(photos))
    //     }else {
            console.log("cache miss");
            const { data } = await axios.get(
                "https://jsonplaceholder.typicode.com/photos",
                { params : { albumId } }
            )
   redisClient.SETEX('photos', DEFAULT_EXPIRATION, JSON.stringify(data));
    //     }
    res.json(data);
})
//})

app.get("/photos/:id",async(req,res) => {
    const { data } = await axios.get(
        `https://jsonplaceholder.typicode.com/photos/${req.params.id}`
        )
    res.json(data);
})

console.log("listening");
app.listen(3000);

if i delete this line its working but can't do nothing without this line. help me with this.

1 Answers1

1

Looks like you forgot to actually connect your client through client.connect():

So in your case you should add something like:

const Redis = require("redis");

let redisClient;

(async () => {
  redisClient = Redis.createClient();

  redisClient.on('error', (err) => console.log('Redis Client Error', err));

  await redisClient.connect();
  
})();

See the docs for further details: https://github.com/redis/node-redis#basic-example

eol
  • 23,236
  • 5
  • 46
  • 64
  • 2
    It solves some issues and gives me an another error like this. node:271309) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'setex' of undefined at app.get (/home/aathilingam/Desktop/Laravel - Docker/rediscache/index.js:38:17) at process._tickCallback (internal/process/next_tick.js:68:7) (node:271309) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:271309) –  Nov 30 '21 at 11:54
  • Please edit your question and add the updated code. – eol Nov 30 '21 at 11:56
  • Edited please help me with that and thanks –  Nov 30 '21 at 12:17
  • You forgot to call your IIFE-function. Add `();` at the end of it, as I did in my example. – eol Nov 30 '21 at 13:35
  • It gives reference error updated code and error above –  Dec 01 '21 at 04:02