如何解决在 getServerSideProps 中使用 redis 导致错误 net.isIP is not a function
如果我错了,请纠正我,但 getServerSideProps
用于在每次渲染时预渲染数据?如果我在 redis
中使用标准的 getServerSideProps
npm 模块,我会收到错误 net.isIP is not a function
。根据我的研究,这是由于客户端尝试使用 redis
函数。
我正在尝试创建一个应用程序,将会话数据保存在基于 cookie 令牌的 redis
键中。根据用户 ID 调用数据库并将数据呈现给组件。我可以在 getServerSideProps
中获取 cookie 令牌,但我运行 client.get(token)
我在运行时收到错误 net.isIP is not a function
。
我是否没有正确使用 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
解决方法
我从 next
升级到 10.0.6
版本 9.3
,但我不再收到错误消息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。