5

Everyone,

I'm trying to setup my first NestJS application. It is backed by Serverless on AWS.

I created a simple Controller that has a Service as a dependency. When I hit the endpoint with my HTTP Client, the object that should contain the Service instance is undefined. I'm not able to make it work. Could you help?

handler.ts

import { Context, Handler } from 'aws-lambda'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './src/module'; import { Server } from 'http'; import { ExpressAdapter } from '@nestjs/platform-express'; import * as serverless from 'aws-serverless-express'; import * as express from 'express'; import {DB} from './src/libs/db'; let cachedServer: Server; function bootstrapServer(): Promise<Server> { const expressApp = express(); const adapter = new ExpressAdapter(expressApp); return NestFactory.create(AppModule, adapter) .then(app => app.enableCors()) .then(app => app.init()) .then(() => DB.connect()) .then(() => serverless.createServer(expressApp)); } export const handle: Handler = (event: any, context: Context) => { if (!cachedServer) { bootstrapServer().then(server => { cachedServer = server; return serverless.proxy(server, event, context); }); } else { return serverless.proxy(cachedServer, event, context); } }; 

module.ts

import { Module } from '@nestjs/common'; import { EventController } from './event.controller'; import { EventService } from './event.service'; @Module({ controllers: [EventController], providers: [EventService], }) export class AppModule {} 

event.service.ts

import { Injectable } from '@nestjs/common'; interface Event{} @Injectable() export class EventService { create(event: Event) { return { id: Date.now() } } } 

event.controller.ts

import { Controller, Post, Body } from '@nestjs/common'; import { EventService } from './event.service'; interface Event { } @Controller('event') export class EventController { constructor(private readonly eventService: EventService) { } @Post() async create(@Body() req) { this.eventService.create(req); } } 

So this.eventService is always undefined. What is wrong with this implementation?

3
  • Everything seems ok to me at the first glance, what about your tsconfig ? Could your share it ? Commented Jun 3, 2019 at 5:32
  • Are you sure, that you really need Nest.js inside AWS Lambda? It increase your "cold starts". Potentially your users/front-end will wait time-to-time to 10 seconds for response from Lambda. Commented Jun 4, 2019 at 6:44
  • Hi Victor I am trying to setup my nestjs project with AWS lambda, I am unable to get any specific documentation regarding the integration of nestjs with AWS, could you give me some information or any particular documentation you followed? Thank you Commented Dec 1, 2019 at 23:40

1 Answer 1

1

Maybe you are missing a line from tsconfig add this below:

"emitDecoratorMetadata": true

Credits to the God of Nestjs Mr. Kamil's reply: https://stackoverflow.com/a/50121886/6301493

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.