gpt4 book ai didi

javascript - 未处理的 promise 拒绝警告 : Error: The client is closed in NodeJS and Redis

转载 作者:行者123 更新时间:2023-12-05 00:39:09 24 4
gpt4 key购买 nike

我正在实现 Access TokenRefresh Token使用 JWT NodeJS中的登录功能应用。我正在尝试使用 Redis存储Refresh Token ,但我得到以下代码:
redis.js

const redis = require('redis');

require('dotenv').config({ path : __dirname + '/../config/env/.env' });

let redisClient;

redisClient = redis.createClient(6379, '127.0.0.1');

module.exports = redisClient;
authRoute.js
'use strict';

const JwtUtil = require('../../middlewares/jwt-util');
const redisClient = require('../../middlewares/redis');

const authRouter = require('express').Router();

const jwt = new JwtUtil();

authRouter.post('/user-token/generate', (req, res, next) => {
const accessToken = jwt.sign(req.body);

const refreshToken = jwt.refresh();

redisClient.set(req.body.id, refreshToken);

res.status(200).send({
result : 'success',
data : {
accessToken,
refreshToken
}
});
});

module.exports = authRouter;
我打印了几份日志,但似乎 redisClient.set(req.body.id, refreshToken);authRoute.js .里面好像有错误。

UnhandledPromiseRejectionWarning: Error: The client is closed

(node:24640) UnhandledPromiseRejectionWarning: Unhandled promiserejection. This error originated either by throwing inside of an asyncfunction without a catch block, or by rejecting a promise which wasnot handled with .catch(). To terminate the node process on unhandledpromise rejection, use the CLI flag --unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).(rejection id: 2) (node:24640) [DEP0018] DeprecationWarning: Unhandledpromise rejections are deprecated. In the future, promise rejectionsthat are not handled will terminate the Node.js process with anon-zero exit code.

Access TokenRefresh Token正常发出。此外,以下代码不起作用。我不确定我应该如何重写代码。我应该怎么办?
redisClient.on('connection', () => { 
console.info('Redis connected!');
}
虽然我使用的是 Windows , 我下载了 Redis , 执行了 ping测试,并确认服务器正在运行。

最佳答案

至于现在我能够部分解决获取数据,但是对于 redis v4 仍然没有完成设置数据。 #redis #v4 #redisv4

app.get('/data',(req,res) => {
// check in redis first
//console.log(">>>>url",url)
(async () => {
const userInput = (req.query.country).trim()
const url = `https://en.wikipedia.org/w/api.php?action=parse&format=json&section=0&page=${userInput}`

const client = createClient({
host:'localhost',
port:6379});

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

await client.connect();

const response = await client.get(`${userInput}`)
if(response){
const output = JSON.parse(response);
res.send(output)
}
})();

})

关于javascript - 未处理的 promise 拒绝警告 : Error: The client is closed in NodeJS and Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70197001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com