redis - 在 getServerSideProps 中使用 redis 会导致错误 net.isIP is not a function

标签 redis next.js server-side

如果我错了,请纠正我,但是 getServerSideProps 用于在每个渲染上预渲染数据?如果我在 getServerSideProps 中使用标准 redis npm 模块,则会收到错误 net.isIP is not a function。根据我的研究,这是由于客户端尝试使用 redis 功能造成的。

我正在尝试创建一个应用程序,将 session 数据保存在基于 cookie token 的 redis 键中。根据用户 ID 调用数据库并将数据呈现给组件。我可以在 getServerSideProps 中获取 cookie token ,但我运行 client.get(token) 我收到错误 net.isIP is not a function at运行时。

我是否没有正确使用 getServerSideProps 或者我应该使用不同的方法/函数?我对整个 Next.js 世界都是新手。我很感谢您的帮助。

如果我在 /api 路由中使用相同的功能,一切都会正常工作。

import util from 'util';
import client from '../libs/redis' // using 'redis' module

// ... my component here

export async function getServerSideProps(context) {
  const get = util.promisify(client.get).bind(client);
  
  const name = await get('mytoken') // error `net.isIP is not a function`

  return {
    props: {
      name
    },
  }
}
// redis.js
const redis = require("redis");
const client = redis.createClient();
 
client.on("error", function(error) {
  console.error(error);
});

module.exports = client

最佳答案

我从 9.3 升级到 next 版本 10.0.6,并且不再收到该错误。

关于redis - 在 getServerSideProps 中使用 redis 会导致错误 net.isIP is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65994341/

相关文章:

node.js - redis session 在服务器上不工作

caching - 序列化和反序列化 POJO 的开销是否是使用 Infinispan 而不是 Memcached 或 Redis 来缓存 POJO 的一个很好的理由?

javascript - NextJS 项目中使用的 ReactJS 库表示不是构造函数

C# - Websocket - 将消息发送回客户端

Redis 服务器未启动 - Forked Process 未及时响应

python - 使用 redis-py 更改 PubSub 订阅

reactjs - 面包屑和 NextJS

amazon-s3 - 上传图像 - 数据显示为 "���"�!1A"Qaq�2�B�#"并且图像为空白 - Next.js 应用程序上传到 DigitalOcean Spaces/AWS S3

debugging - meteor 服务器端断点从未在cloud9中命中

java程序客户端触发服务器端java程序