0

在尝试获取数据时运行 redis 时,它会显示这种错误,这是什么问题。

   (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.

我的代码用于从网站以 json 格式获取照片的详细信息,当我尝试将它们加载到 redis 中时,出现上述错误。我的代码是

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);

如果我删除这条线,它的工作但没有这条线就什么也做不了。帮我解决这个问题。

4

1 回答 1

0

看起来您忘记通过以下方式实际连接您的客户端client.connect()

因此,在您的情况下,您应该添加以下内容:

const Redis = require("redis");

let redisClient;

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

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

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

有关详细信息,请参阅文档:https ://github.com/redis/node-redis#basic-example

于 2021-11-30T11:20:35.257 回答