大家好,我正在尝试将实体保存到 Redis 存储库中,驱动程序已正确连接,模式创建为与存储库相同的预期。当我尝试保存实体时,问题就出现了,我得到的异常是:
TypeError: this[#schema].generateId is not a function at Repository.save
。
我已经尝试过使用旧的 redis-om 版本的其他模式类型
Class Session extends Entity {}
但每次都会遇到相同的错误。
redis-node-om
的版本是0.4.3
。
这里我提供了不起作用的代码:
const sessionRepository = getSessionRepo(redisClient);
const session = createSessionObj(req);
console.log("[REDIS] **GUARDANDO DATOS EN EL REPOSITORIO DE REDIS**");
const storedSession = await sessionRepository.save(session);
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" }
);
function getSessionRepo(redisClient) {
console.log(sessionSchema);
const sessionRepository = new Repository(sessionSchema, redisClient);
console.log(sessionRepository);
return sessionRepository;
}
function createSessionObj(req) {
let session = {};
session.userId = req.body.userId;
session.role = req.body.role;
session.jwt = req.body.tokenJWT;
return session;
}
在这种情况下错误有所不同:
'TypeError: Cannot read properties of undefined (reading 'forEach')
看来根本问题在于
redis-om
库对其内部属性的处理。具体来说,似乎有必要访问 this.#schema.sessionSchema
来调用 generateId()
并使用 forEach
迭代字段。