当运行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);如果我删除这一行,它可以工作,但是没有这一行就什么也做不了。帮我解决这个问题。
发布于 2021-11-30 11:20:35
看起来你忘记了通过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
https://stackoverflow.com/questions/70168015
复制相似问题