lzztt / koa-session-minimal

Minimal implementation of session middleware for Koa 2
MIT License
75 stars 13 forks source link

cicyle dependecies #5

Closed yuu2lee4 closed 7 years ago

yuu2lee4 commented 7 years ago

when i use koa-generic-session-mongo with koa-session-minimal in koa2, it tell me cicyle dependecies image this is my app.js: image and i just get a user from mongodb , and set it to session.user: image my node version is 7.2.1,and i use it with --harmony to handle async function.

lzztt commented 7 years ago

Hi yuu2lee4,

Nothing indicates that this is a session middleware issue. I created a simple test example with mongo store and tested with node/v7.2.1 with the --harmony option. The session works fine. Can you test with example/07_mongodb_store.js?

Here is the shell output of my test:

# clone repo
ikki@dev:/tmp$ git clone https://github.com/longztian/koa-session-minimal.git
... ...

ikki@dev:/tmp$ cd koa-session-minimal/

# install dependencies
ikki@dev:/tmp/koa-session-minimal$ npm install
... ...

# node version
ikki@dev:/tmp/koa-session-minimal$ node -v
v7.2.1

# build dist and run tests
ikki@dev:/tmp/koa-session-minimal$ make 

# mongodb record before running example
ikki@dev:/tmp/koa-session-minimal$ echo 'db.sessions.find()' | mongo
MongoDB shell version: 2.6.12
connecting to: test
{ "_id" : ObjectId("584c639b40a525d58ea26ae9"), "count" : 12, "sid" : "koa:sess:eGyFRjiJ0TwJykty_0-RNJG4sR9itXSg", "ttl" : ISODate("2016-12-11T20:56:05.598Z") }
bye

# start example server and visit localhost:3000 from a browser
ikki@dev:/tmp/koa-session-minimal$ node --harmony example/07_mongodb_store.js 
^C

# mongodb record after running example
ikki@dev:/tmp/koa-session-minimal$ echo 'db.sessions.find()' | mongo
MongoDB shell version: 2.6.12
connecting to: test
{ "_id" : ObjectId("584c639b40a525d58ea26ae9"), "count" : 15, "sid" : "koa:sess:eGyFRjiJ0TwJykty_0-RNJG4sR9itXSg", "ttl" : ISODate("2016-12-11T20:58:06.751Z") }
bye
lzztt commented 7 years ago

Hi yuu2lee4,

I just got a hint from this ticket. The error message showed that bson library could not serialize the data (before putting it into MongoDB session collection). The user is a MongoDB document which may be deeply bond to a MongoDB record in the database. Now since you were using mongo store as your session store, you were trying to save a document from the user collection into the session collection. I guess the mongo store uses bson to serialize data, then it hits a circular serialization when trying to serialize some internal data of your user object.

You can either use a different session store, or convert your user object to a plain javascript object (with any fields you want to keep) and save it to ctx.session.user. like below:

ctx.session.user = {
    id: user._id,
    name: user.name,
}

I prefer the second way. So that you know exactly what kind of data (after serialization) is saved to your session store. Let me know if this works for you.

yuu2lee4 commented 7 years ago

many tks, the second way is worked