Search code examples
redisnext.jsserver-side

using redis in getServerSideProps results in error net.isIP is not a function


Correct me if I am wrong but getServerSideProps is used to pre-render data on each render? If I use the standard redis npm module in getServerSideProps I get the error net.isIP is not a function. From what I have researched this is due to the client trying to use the redis functions.

I am trying to create an application to where session data is saved in a redis key based on a cookie token. Based on the user Id a database is called and renders data to the component. I can get the cookie token in getServerSideProps but I I run client.get(token) I get the error net.isIP is not a function at runtime.

Am I not using getServerSideProps correctly or should I be using a different method / function? I am new to the whole Next.js world. I appreciate the help.

If I use the same functionality in an /api route everything works correctly.

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

Solution

  • I upgraded to next version 10.0.6 from 9.3 and I do not receive the error anymore.