Secbone / koa-session2

Middleware for Koa2 to get/set session
MIT License
153 stars 30 forks source link

Hi,I add koa-session2 Middleware,but when use session ,make error #1

Closed soulxy closed 8 years ago

soulxy commented 8 years ago

I followed your demo to add my middleware, found some problem.

my code:


TypeError: Cannot set property 'view' of undefined at app.js:36:3 at dispatch (E:\mywork\koa2.0\node_modules\koa\node_modules\koa-compose\in dex.js:43:32) at next (E:\mywork\koa2.0\node_modules\koa\node_modules\koa-compose\index. js:44:18) at app.js:31:9 at [object Generator].next (native) at step (app.js:23:1) at app.js:23:1 at new Promise (E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-po lyfill\node_modules\core-js\modules\es6.promise.js:193:7) at app.js:23:1 at app.js:27:9 at dispatch (E:\mywork\koa2.0\node_modules\koa\node_modules\koa-compose\in dex.js:43:32) at next (E:\mywork\koa2.0\node_modules\koa\node_modules\koa-compose\index. js:44:18) at E:\mywork\koa2.0\node_modules\koa-bodyparser\index.js:60:14 at run (E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-polyfill\n ode_modules\core-js\modules\es6.promise.js:89:22) at E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-polyfill\node_m odules\core-js\modules\es6.promise.js:102:28 at flush (E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-polyfill \node_modules\core-js\modules_microtask.js:14:5) at nextTickCallbackWith0Args (node.js:415:9) at process._tickCallback (node.js:344:13)

Secbone commented 8 years ago

@soulxy

koa-session2 has already supported koa2, so you do not need to convert it, just use like this

app.use(session({store:new Store()}))

do not use convert middleware

soulxy commented 8 years ago

I known it,but server response timeout by your demo . So I have to use async/await, Service can respond, while Cannot set property 'view' of undefined .... I am a new koa ,use convert ,just try

soulxy commented 8 years ago

Sorry,Sorry ...I did a very foolish thing, forgot to start redis, then faced to anther error


TypeError: Cannot assign to read only property 'userObj' of [object Object] at users.js:20:7 at [object Generator].next (native) at step (E:\mywork\koa2.0\routers\users.js:3:191) at E:\mywork\koa2.0\routers\users.js:3:368 at run (E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-polyfill\node_modules\core-js\modules\es6.promise.js:89:22) at E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-polyfill\node_modules\core-js\modules\es6.promise.js:102:28 at flush (E:\mywork\koa2.0\node_modules\runkoa\node_modules\babel-polyfill\node_modules\core-js\modules_microtask.js:14:5) at nextTickCallbackWith0Args (node.js:415:9) at process._tickCallback (node.js:344:13)

Secbone commented 8 years ago

@soulxy It seems an error in your router file users.js, maybe you can check E:\mywork\koa2.0\routers\users.js and find it. :)

soulxy commented 8 years ago

@Secbone in users.js , I just write ctx.session.userobj = '...' ... I tried my best to find question, but just find console.log('typeof session', typeof ctx.session) , print session is string . And I try to modify your files , Unfortunately, does not find a solution

Secbone commented 8 years ago

@soulxy Could you show me the users.js file ? I think there is something wrong in the userobj

soulxy commented 8 years ago

I writed it in jsbin... app.js users.js

Secbone commented 8 years ago

@soulxy Could you also show me the store.js file? I suspect there may have a problem in set method.

soulxy commented 8 years ago

I copy yours store.js

Secbone commented 8 years ago

@soulxy It seems good, I don't know what's wrong with your code, so I wrote a test for the custom store, and it works well. Maybe you can pull the repo and check it. Commit detail

soulxy commented 8 years ago

@Secbone I resolve this issue :) ~~ There are some issues in your get function of Store.js... At first time, typeof ctx.session is object,But when you got this type second time, it became string.Since reading redis's value without parsing, "[object object]" appears

Secbone commented 8 years ago

@soulxy Good! I just thought somebody's session was not a object, maybe a string, so I didn't check the type of it. How do you think if I check the type?

soulxy commented 8 years ago

?? I just parse this value in get function of Store.js , will be fine :)