首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用redis for data client执行节点时出现问题

使用redis for data client执行节点时出现问题
EN

Stack Overflow用户
提问于 2021-11-30 10:45:27
回答 1查看 798关注 0票数 0

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

代码语言:javascript
复制
   (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中时,我得到了上面的错误。我的代码是

代码语言:javascript
复制
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);

如果我删除这一行,它可以工作,但是没有这一行就什么也做不了。帮我解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2021-11-30 11:20:35

看起来你忘记了通过client.connect()连接你的客户端

因此,在您的示例中,您应该添加如下内容:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70168015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档