在getServerSideProps中使用redis会导致错误网.isIP不是一个函数



如果我错了,请纠正我,但getServerSideProps用于每次渲染上的预渲染数据?如果我在getServerSideProps中使用标准redisnpm模块,我会得到错误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

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

最新更新