redis 版本4.0.4报错

来源:9-3 开发路由(下)

给生活一个大大的傻笑

2022-02-24

老师,redis4.0.4 设置redis,报错,尝试了课程中 2x版本的没有问题,自己尝试解决了下,但是没成功,老师方便看下嘛

(node:84664) UnhandledPromiseRejectionWarning: Error: The client is closed
at Commander._RedisClient_sendCommand (D:\learn\koa2-weibo\node_modules@node-redis\client\dist\lib\client\index.js:408:31)
at Commander.commandsExecutor (D:\learn\koa2-weibo\node_modules@node-redis\client\dist\lib\client\index.js:166:154)
at Commander.BaseClass. [as get] (D:\learn\koa2-weibo\node_modules@node-redis\client\dist\lib\commander.js:8:29)
at D:\learn\koa2-weibo\src\cache_redis.js:37:17
at new Promise ()
at get (D:\learn\koa2-weibo\src\cache_redis.js:36:19)
at getSquareCaCheList (D:\learn\koa2-weibo\src\cache\blog.js:22:29)
at getSquareBlogList (D:\learn\koa2-weibo\src\controller\blog-square.js:6:24)
at D:\learn\koa2-weibo\src\routes\view\index.js:55:24
at dispatch (D:\learn\koa2-weibo\node_modules\koa-router\node_modules\koa-compose\index.js:44:32)
at next (D:\learn\koa2-weibo\node_modules\koa-router\node_modules\koa-compose\index.js:45:18)
at loginRedirect (D:\learn\koa2-weibo\src\middlewares\loginChecks.js:30:11)
at dispatch (D:\learn\koa2-weibo\node_modules\koa-router\node_modules\koa-compose\index.js:44:32)
at next (D:\learn\koa2-weibo\node_modules\koa-router\node_modules\koa-compose\index.js:45:18)
at D:\learn\koa2-weibo\node_modules\koa-router\lib\router.js:346:16
at dispatch (D:\learn\koa2-weibo\node_modules\koa-router\node_modules\koa-compose\index.js:44:32)
(Use node --trace-warnings ... to show where the warning was created)
(node:84664) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:84664) [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.
图片描述

写回答

3回答

慕粉2320486108

2022-03-21

/**
 * @description 连接redis方法 get set
 * @author
 */

const { createClient } = require('redis')
const { REDIS_CONF } = require('../conf/db')

// 创建客户端
let redisClient;
(async () => {
redisClient = createClient()
redisClient.on('error', (err) => console.log('Redis Client Error', err))
await redisClient.connect()
})()

// set
/**
 * redis set
 * @param {String} key key
 * @param {String} val val
 * @param {Number} timeout 过期时间 单位秒
 */
async function set(key, val, timeout = 60 * 60) {
if (typeof val === 'object') {
val = JSON.stringify(val)
}
await redisClient.set(key, val, {
EX: timeout
})
}

// get
/**
 * redis get
 * @param {string} key 
 */
async function get(key) {
const result = await redisClient.get(key)
return JSON.parse(result)
}

module.exports = {
set,
get
}


0
0

双越

2022-02-24

你先用 v2.8 ,先吧项目功能跑完,把课程做完。不要因为这单个问题影响了自己的学习进度。

后面我会单独出一节视频,来讲一讲如何升级 4.0

0
0

给生活一个大大的傻笑

提问者

2022-02-24

就是调用 设置redis数据的时候那个 set 方法报错。

await get(key); 

0
0

Node.js+Koa2框架生态实战 - 从零模拟新浪微博

用 Koa2 做真正的实战项目,不再只是增删改查的小白工程师

964 学习 · 388 问题

查看课程