Node Redis-OM: this[#schema].generateId is not a function

ghz 9months ago ⋅ 129 views

Hi everyone I am trying to save an entinty into a redis repository, the driver is correctly connected the schema is created as edxpected same with the repo. The problem comes when I try to save the entity, The exception I am getting is: TypeError: this[#schema].generateId is not a function at Repository.save.

I already tried with other schema types of old redis-om version usind Class Session extends Entity {} but I get the same error every time.

the versión of redis-node-om is 0.4.3.

Here I provide the code that isn't working:

Middleware.js

const sessionRepository = getSessionRepo(redisClient);
const session = createSessionObj(req);
console.log("[REDIS] **GUARDANDO DATOS EN EL REPOSITORIO DE REDIS**");
const storedSession = await sessionRepository.save(session);

sessionSchema

const sessionSchema = new Schema(
    "session",
    {
        userId: { type: "string", field: "user_id" },
        role: { type: "string", field: "user_role" },
        jwt: { type: "string", sortable: "yes", field: "user_jwt", weight: 1 },
    },
    { dataStructure: "HASH" }
);

getSessionRepo function

function getSessionRepo(redisClient) {
    console.log(sessionSchema);
    const sessionRepository = new Repository(sessionSchema, redisClient);
    console.log(sessionRepository);
    return sessionRepository;
}

createSession function:

function createSessionObj(req) {
    let session = {};
    session.userId = req.body.userId;
    session.role = req.body.role;
    session.jwt = req.body.tokenJWT;

    return session;
}

Answers

The error you're encountering, TypeError: this[#schema].generateId is not a function, suggests that there might be an issue with how you're initializing the schema or repository in your code. Let's troubleshoot the possible causes:

  1. Schema Initialization: Make sure that the schema is initialized correctly before trying to create a repository with it. Ensure that the schema is properly defined and that it has the necessary methods, including generateId. The generateId method is typically used to generate unique identifiers for entities.

  2. Repository Initialization: Ensure that the repository is being created with the correct schema and Redis client. Double-check the arguments being passed to the Repository constructor to ensure that they are correct.

  3. Entity Structure: Verify that the structure of the entity you're trying to save aligns with the schema definition. Make sure that the entity object includes all the required fields defined in the schema.

  4. Library Compatibility: Ensure that the version of redis-node-om you're using (0.4.3) is compatible with the way you're initializing schemas and repositories. There might be changes or differences in usage between versions that could be causing issues.

Based on the provided code snippets, everything seems to be in order, but it's possible that there's a subtle issue elsewhere in your codebase. Here are a few additional steps you can take to debug the issue:

  • Check the documentation and examples provided by redis-node-om to see if there are any specific requirements or conventions you need to follow.
  • Look for similar issues or discussions on forums or GitHub repositories related to redis-node-om to see if others have encountered similar problems.
  • Try simplifying your code and isolating the problematic code snippet to narrow down the issue.
  • Consider reaching out to the maintainers of redis-node-om for assistance or clarification if you're unable to resolve the issue on your own.

By carefully reviewing your code and considering these troubleshooting steps, you should be able to identify and resolve the issue with saving entities to the Redis repository.