如果我错了,请纠正我,但是 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/