Unable to set cookie in nestjs, TypeError: Cannot set properties of undefined (setting 'color')
Unable to set cookie in nestjs, TypeError: Cannot set properties of undefined (setting 'color')
我正在尝试在会话中设置 属性 但未定义。使用 NestJs + Fastify
app.module.ts
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
// import fastifyCookie, { FastifyCookieOptions } from 'fastify-cookie'; --this was also not workin
const cookieSession = require( 'cookie-session' );
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter( )
);
app.register( cookieSession( {
keys: [ 'asdsds' ]
} ) );
lines...
}
bootstrap();
users.controller.ts
imports...
export class UsersController {
constructor( private usersService: UsersService, private authService: AuthService ) { }
@Get( '/colors/:color' )
setColor( @Param( 'color' ) color: string, @Session( ) session: any ) {
// console.log( 'check ', session ); --> UNDEFINED
session.color = color; --> Error HERE
// session.set( color );
}
@Get( '/colors' )
getColor( @Session( ) session: any ) {
console.log( 'get session: ', session );
return session.color;
}
错误是:
[Nest] 41229 - 13/02/2022, 9:51:06 pm ERROR [ExceptionsHandler] Cannot set properties of undefined (setting 'color')
TypeError: Cannot set properties of undefined (setting 'color')
at UsersController.setColor (/home/prabhat/Practice/Nest-Fastify/nestfastify/src/users/users.controller.ts:20:22)
at /home/prabhat/Practice/Nest-Fastify/nestfastify/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:96:5)
我也试过
// app.register(fastifyCookie );
// app.register(fastifySession, {secret: 'a secret with minimum length of 32 characters'});
cookie-session
是一个快速中间件。所以如果它与 fastify 一起使用,你可能会得到 req.raw.session
作为 cookie。
fastify-cookie
是 cookie 支持的 fastify 特定包,但不是直接的会话,所以你需要使用 req.cookie
或制作一个 @Cookie()
装饰器
@fastify/session
是fastify中session支持的包。通过将 @fastify/session
与 fastify-cookie
一起使用,您将可以访问 req.session
和 req.cookie
对我有用的是fastify-secure-session
包,提到here
现在我的 main.ts 看起来像:
imports..
import secureSession from 'fastify-secure-session';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter( )
);
app.register(secureSession, {
secret: 'averylogphrasebiggerthanthirtytwochars',
salt: 'mq9hDxBVDbspDR6n',
});
lines..
}
bootstrap();
而app.controller.ts是:
imports..
import * as secureSession from 'fastify-secure-session'
@Controller( 'auth' )
@MongooseClassSerializerInterceptor( UserDto )
export class UsersController {
constructor( private usersService: UsersService, private authService: AuthService ) { }
@Get( '/colors/:color' )
setColor( @Param( 'color' ) color: string, @Session( ) session: secureSession.Session ) {
console.log( 'check ', session );
// session.color = color;
session.set('color', color)
}
@Get( '/colors' )
getColor( @Session( ) session: secureSession.Session ) {
console.log( 'get session: ', session );
// return session.color;
return session.get( 'color' );
}
}
有了这个我就可以在带有 Fastify 的 NestJs 中设置 cookie。
仍然想知道为什么以前的方法对我不起作用,正如 @Jay 在他们的回答中提到的那样。
我正在尝试在会话中设置 属性 但未定义。使用 NestJs + Fastify
app.module.ts
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
// import fastifyCookie, { FastifyCookieOptions } from 'fastify-cookie'; --this was also not workin
const cookieSession = require( 'cookie-session' );
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter( )
);
app.register( cookieSession( {
keys: [ 'asdsds' ]
} ) );
lines...
}
bootstrap();
users.controller.ts
imports...
export class UsersController {
constructor( private usersService: UsersService, private authService: AuthService ) { }
@Get( '/colors/:color' )
setColor( @Param( 'color' ) color: string, @Session( ) session: any ) {
// console.log( 'check ', session ); --> UNDEFINED
session.color = color; --> Error HERE
// session.set( color );
}
@Get( '/colors' )
getColor( @Session( ) session: any ) {
console.log( 'get session: ', session );
return session.color;
}
错误是:
[Nest] 41229 - 13/02/2022, 9:51:06 pm ERROR [ExceptionsHandler] Cannot set properties of undefined (setting 'color')
TypeError: Cannot set properties of undefined (setting 'color')
at UsersController.setColor (/home/prabhat/Practice/Nest-Fastify/nestfastify/src/users/users.controller.ts:20:22)
at /home/prabhat/Practice/Nest-Fastify/nestfastify/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:96:5)
我也试过
// app.register(fastifyCookie );
// app.register(fastifySession, {secret: 'a secret with minimum length of 32 characters'});
cookie-session
是一个快速中间件。所以如果它与 fastify 一起使用,你可能会得到 req.raw.session
作为 cookie。
fastify-cookie
是 cookie 支持的 fastify 特定包,但不是直接的会话,所以你需要使用 req.cookie
或制作一个 @Cookie()
装饰器
@fastify/session
是fastify中session支持的包。通过将 @fastify/session
与 fastify-cookie
一起使用,您将可以访问 req.session
和 req.cookie
对我有用的是fastify-secure-session
包,提到here
现在我的 main.ts 看起来像:
imports..
import secureSession from 'fastify-secure-session';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter( )
);
app.register(secureSession, {
secret: 'averylogphrasebiggerthanthirtytwochars',
salt: 'mq9hDxBVDbspDR6n',
});
lines..
}
bootstrap();
而app.controller.ts是:
imports..
import * as secureSession from 'fastify-secure-session'
@Controller( 'auth' )
@MongooseClassSerializerInterceptor( UserDto )
export class UsersController {
constructor( private usersService: UsersService, private authService: AuthService ) { }
@Get( '/colors/:color' )
setColor( @Param( 'color' ) color: string, @Session( ) session: secureSession.Session ) {
console.log( 'check ', session );
// session.color = color;
session.set('color', color)
}
@Get( '/colors' )
getColor( @Session( ) session: secureSession.Session ) {
console.log( 'get session: ', session );
// return session.color;
return session.get( 'color' );
}
}
有了这个我就可以在带有 Fastify 的 NestJs 中设置 cookie。
仍然想知道为什么以前的方法对我不起作用,正如 @Jay 在他们的回答中提到的那样。