Modern Full-Stack Development with Nest.js, React, TypeScript, and MongoDB: Part 1

Learn how to build modern and secure Full-Stack applications with React, TypeScript, and Nest.js.

Read on

4 Likes

Thanks for reading! What are your thoughts on Building Modern Full-Stack Development with Nest.js, React, TypeScript and MongoDB. This is the first part of the series and it focuses on building the backend API with Nest.js. Feel free to post your comments.

1 Like

Thank you very much! Can’t wait to see the next chapter!

We will let you know once it’s there!

hi, currently I am working on your tutorial, but when I am creating the service, I am getting this error:

[Nest] 48700 - 03/25/2020, 11:12:39 PM [ExceptionHandler] Nest can’t resolve dependencies of the BlogService (?). Please make sure that the argument PostModel at index [0] is available in the BlogModule context.

Potential solutions:

  • If PostModel is a provider, is it part of the current BlogModule?
  • If PostModel is exported from a separate @Module, is that module imported within BlogModule?
    @Module({
    imports: [ /* the Module containing PostModel */ ]
    })

Pinging @yemiwebby for visibility :slight_smile:

2 Likes

Hello @eddzmaciel. Thanks for reading!

  1. PostModel became part of the BlogModule immediately after it was registered within the BlogModule.
  2. Once you import a particular module into another. It will inherit all its dependencies and have access to all its functionalities as well. In this case, you can define PostModel in a different Module and import that Module into BlogModule.

But I will advise that you define and set up the configuration of PostModel within the root Module (i.e AppModule()) if you prefer to give it global scope.

I hope that answers your question? Thanks for the ping @konrad.sopala

1 Like

the service is already imported on blog.module.ts, I copied exactly the same code that you have in the tutorial.

-----blog.module.ts:

import { Module } from ‘@nestjs/common’;
import { BlogService } from ‘./blog.service’;

@Module({
providers: [BlogService],
})
export class BlogModule {}

----blog.service.ts:

// /blog-backend/src/blog/blog.service.ts
import { Injectable } from ‘@nestjs/common’;
import { Model } from ‘mongoose’;
import { InjectModel } from ‘@nestjs/mongoose’;
import { Post } from ‘./interfaces/post.interface’;
import { CreatePostDTO } from ‘./dto/create-post.dto’;

@Injectable()
export class BlogService {
constructor(@InjectModel(‘Post’) private readonly postModel: Model) {}

async addPost(createPostDTO: CreatePostDTO): Promise {
const newPost = await this.postModel(createPostDTO);
return newPost.save();
}

async getPost(postID): Promise {
const post = await this.postModel.findById(postID).exec();
return post;
}

async getPosts(): Promise<Post> {
const posts = await this.postModel.find().exec();
return posts;
}
}

Oh, I see. Please check the tutorial again. If you followed to the end, you will realized that the blogModule was later updated and an explanation provided for it. This is what it looks like towards the end of the tutorial:

-----blog.module.ts:
import { Module } from ‘@nestjs/common’;
import { BlogService } from ‘./blog.service’;
import { BlogController } from ‘./blog.controller’;
import { MongooseModule } from ‘@nestjs/mongoose’; // add this
import { BlogSchema } from ‘./schemas/blog.schema’; // and this

@Module({
imports: [
MongooseModule.forFeature([{ name: ‘Post’, schema: BlogSchema }]),
], // add
providers: [BlogService],
controllers: [BlogController]
})
export class BlogModule {}

2 Likes

Thanks for providing the explanation @yemiwebby!

1 Like

thank you very much, I will check, lets see what happen

2 Likes

For those that get the error “Error: algorithms should be set” from express-jwt, the following will be useful:

It seems that the latest version of express-jwt does not like RS256.

2 Likes

Thanks for sharing that with the rest of community @shafique.jamal!

The issue is that the guide lists the algorithm as algorithm: ['RS256'], when it should be algorithms: ['RS256'],

I was able to get it to work by adding the s to the end of algorithms

3 Likes

Glad you have it working and thanks for sharing with the rest of community!

I was actually here for this, for the solution @Skezey

Can it be corrected in the tutorial @yemiwebby ?

2 Likes

I contacted our content team regarding that.

1 Like

Howdy! I apologize for the late reply. We’ll fix this on the live post :+1:

1 Like

Thanks @Skezey!! that comment was just what I needed to solve the issue

1 Like