kleydon / prisma-session-store

Express session store for Prisma
MIT License
116 stars 18 forks source link

[mongodb] Raw query failed error when trying to access the session object #126

Closed greedyj4ck closed 1 month ago

greedyj4ck commented 1 month ago

Hello. I am having an issue every time express-session need to access information from database. Sessions are being created/destroyed correctly but I am having constant errors in my express app logs. Seems there is a problem with parsing data stored.

Using: "prisma": "^5.12.1", "@prisma/client": "^5.12.1", "@quixo3/prisma-session-store": "^3.1.13",

PrismaClientKnownRequestError:
Invalid `this.prisma[this.sessionModelName].update()` invocation in
D:\GIT\PROJECTS\elf\node_modules\@quixo3\prisma-session-store\dist\lib\prisma-session-store.js:617:81
  614 existingSession = _b.sent();
  615 if (!(existingSession !== null)) return [3 /*break*/, 5];
  616 existingSessionData = __assign(__assign({}, this.serializer.parse((_a = existingSession.data) !== null && _a !== void 0 ? _a : '{}')), { cookie: session.cookie });
→ 617 return [4 /*yield*/, this.prisma[this.sessionModelName].update(
Raw query failed. Code: `unknown`. Message: `Kind: Command failed: Error code 14 (TypeMismatch): BSON field 'update.updates.u' is the wrong type 'array', expected type 'object', labels: {}`
    at In.handleRequestError (D:\GIT\PROJECTS\elf\node_modules\@prisma\client\runtime\library.js:122:6854)
    at In.handleAndLogRequestError (D:\GIT\PROJECTS\elf\node_modules\@prisma\client\runtime\library.js:122:6188)
    at In.request (D:\GIT\PROJECTS\elf\node_modules\@prisma\client\runtime\library.js:122:5896)
    at async l (D:\GIT\PROJECTS\elf\node_modules\@prisma\client\runtime\library.js:127:11167)

My prisma Session model:

model Session {
  id        String   @id @map("_id")
  sid       String   @unique
  data      String
  expiresAt DateTime
}

Example session stored in mongodb:

{
    "_id" : "p8jiKLJa5IRVV16cw2a1j8y5lcqrpGZw",
    "sid" : "p8jiKLJa5IRVV16cw2a1j8y5lcqrpGZw",
    "data" : "{\"cookie\":{\"originalMaxAge\":604800000,\"expires\":\"2024-04-23T10:26:53.063Z\",\"httpOnly\":false,\"path\":\"/\"},\"user\":{\"id\":\"661cf66fd43967459bfc7832\",\"email\":\"email@email.com\",\"firstName\":\"FirstName\",\"lastName\":\"LastName\",\"isAdmin\":true,\"isActivated\":true,\"isStation\":false,\"rolesIDs\":[],\"_authorized\":true}}",
    "expiresAt" : ISODate("2024-04-23T10:26:53.063Z")
}
kleydon commented 1 month ago

Hi @greedyj4ck, Thanks for posting. Any Mongo users out there, who have seen something like this?

greedyj4ck commented 1 month ago

I am attaching RoleId's array within user data, could it be that [] characters are not properly escaped or something ?

greedyj4ck commented 1 month ago

Its a compatibility problem with old mongodb 4.x version. After upgrading to newest one, issue is gone.

kleydon commented 1 month ago

Good to know what the cause was; thanks for posting the solution.