#Mongoose how to validate if user exists with username etc.

74 messages · Page 1 of 1 (latest)

quasi umbra
#

How can i build it that when a user exists with the username which i provide? That i redirect like username already exists? ```ts
import { HydratedDocument } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

export type UserDocument = HydratedDocument<User>;

@Schema({
timestamps: true,
versionKey: false,
})
export class User {
@Prop({ required: true })
firstName: string;

@Prop({ required: true })
lastName: string;

@Prop({ required: true })
username: string;

@Prop({ required: true })
email: string;

@Prop({ required: true })
password: string;

@Prop()
createdAt!: Date;

@Prop()
updatedAt!: Date;
}

export const UserSchema = SchemaFactory.createForClass(User);
ts
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';

export class CreateUserData {
@IsNotEmpty()
@IsString()
readonly firstName: string;

@IsNotEmpty()
@IsString()
readonly lastName: string;

@IsNotEmpty()
@IsString()
readonly username: string;

@IsNotEmpty()
@IsString()
@IsEmail()
readonly email: string;

@IsNotEmpty()
@IsString()
readonly password: string;
}

dusk topaz
#

I'd run a findOne in your service to find by the passed username and if you get a non-null result throw an exception

quasi umbra
#

ok, but i want to show up in frontend like when username exists already

dusk topaz
#

That's why you throw an exception and react to the status 400 in the front end

#

Your server should have 0 knowledge about what the front end does, and should essentially just care about returning every the success data or the error message in a way that's easy for others to read and consume

#

Assuming you have an SPA and not using a template renderer

quasi umbra
quasi umbra
#

okay but is better to use a custome xception? or like httpexception?=

dusk topaz
#

Usually, I just go with the exception that matches the status and add whatever custom message I need, the parse it in the front end

quasi umbra
#

The Problem what i dont understand is, i enter a username in the form of frontend. and then the username is already taken. What i must do? when i send a HttpExceptions it is only a message not more, dont know if wrong or not 😄

#

Like i have a create function (async) in the service of user. like now i must check if user exists with the username or email. How to show in frontend then? Can u explain?

dusk topaz
#

What do you mean? Your server responds with an error, correct? Have the front endf catch and handle that error by displaying a user friendly message

quasi umbra
#

like this.

#

or what ever

dusk topaz
#

That woul;d be dependent on several things:

  1. What front end you're using
  2. If you're using a UI library
  3. How you're sending these requests and handling the responses
#

I can point you to some of my own code using react, materialUI, and fetch, but everyone's setup will be differrent

quasi umbra
#

yep.

#

i use react, with tailwindcss, fetch also.

dusk topaz
#

This isn't really a Nest specific thing either, it's all frontend work here

quasi umbra
#

it is a nest specific

#

its backend related

#

what for error messages are send

#

like i have in mind this from other frameworks: ```ts
errors: [
{
field: 'username',
message: 'Username already taken',
},
{
field: 'email',
message: 'Email already taken',
},
],

dusk topaz
quasi umbra
#

like

dusk topaz
#

It's all about how you manage it in the front end to display what you want

#

The backend gives the general infortmation. How specific is up to you

#

Then the front end parses it and decides how to display it

#

Any time you ask "How tp show user X" that would be UI specific'

#

Maybe the backend needs to send an exception, but the UI is what wiill react to it

dusk topaz
quasi umbra
#

but frontend also hard.. or?

dusk topaz
#

Depends

#

How well do you know JS?

#

Cause that's pretty much what frontend is

#

I don't know what's "hard" for you, to be fair. I find it more challenging because of the design portion and user experience design, but I'm skilled enough with it

quasi umbra
#

i hate that i must build all connections by my self

#

in laravel it gives inertiajs for frontend

dusk topaz
#

Why do you want to keep using Nest if you just keep complaining about how "hard" it is?

quasi umbra
#

i say only that laravel has better support as nest

#

not more

dusk topaz
#

Better support for what?

quasi umbra
#

for all it have more packages then nest. to easilly create backend

#

its so easy

#

but nestjs make all harder

dusk topaz
#

How does Nest make it harder?

#

Because it doesn't have a dedicated front end to work with?

quasi umbra
#

the structure. the core is not so easy to handle like laravel

dusk topaz
#

Because they're entirely different langauges and frameworks with different goals in mind

#

Laravel was created (from what I can tell) to be an "application framework". Meaning full stack integration.

#

Nest on the other hand is a NodeJS framework. Node is a runtime environment for Javascript, separate from the browser. It can be used for command line applications (CLIs) (like with nest-commander), Discord bots (necord), telegram bots (nestjs-telegraf), microservices (built in and community), HTTP, and Websocket communication.

quasi umbra
#

no thats not langauge thing

#

nestjs is build not clearly!

#

and not so supported as laravel

dusk topaz
#

If you can't identify what exactly is objectively "bad" then it's an opinion, right? Not a fact you can just declare

dusk topaz
#

I provide _plenty_of support for Nest on this server, and have for 5 plus year, as well as through improvements to the framework, community packages, stack overflow, blog articles, and much more

dusk topaz
quasi umbra
#

it can very more

dusk topaz
#

Again, that's great. I just took a quick look. Happy to know I was wrong with what I said it can do, and that it can do more 🙂

quasi umbra
#

nestjs debugging is painful, because TypeORM... e.g. also Circular dependencies, Verbose Syntax, Learning Curve3

#

crazy or?

#

laravel is best, laravel has a soooo powerfull ORM 😄

#

why, why, makes nestjs it so hard to show up an error for a form validation? laravel its are 2 lines of code.

dusk topaz
#

nestjs debugging is painful,
What's difficult with it? Should be as quick as running a start:dev command inside a JS debug terminal in VSCode. Or following how any other debug set up works with Typescript in your IDE of choice

because TypeORM
No one forces you to chose TypeORM. I use Kysely for all of my projects. There's sequelize, mikro-orm, knex, orchid, drizzle, take your pick 🙂

Circular dependencies
That's usually a code smell, and a sign that things are too tightly coupled, could be refactored out

Verbose Syntax
The @Module()? Personally, I like it, it tells me what each module will be working with and where to find what I need. Some people don't like it. It was inspired by Angular, which was in turn inspired by Spring.

Learning Curve
People say it's steep, I never had much of an issue getting over it, but that's me. Every learner is different

Laravel has ... powerful ORM
Yeah, but that's PHP vs JS and not really related to Nest now is it?

Laravel is best
I'm gonna ask this respectfully: if you like Laravel so much, why are you forcing yourself to go through with using Nest? Since you've shown up on the server, you've done nothing but complain about how "hard" Nest is and "how much better" Laravel is.

Most of your complaints aren't really even about Nest, but how much more work goes into having separate processes for the front end and back end. Nest was never built to have a dedicated UI, it was built to provide a scalable archiitecture on the server side

quasi umbra
grim geode
#

@quasi umbra - Sad you feel that way. Nest isn't bad. In fact, it is quite good and powerful and useful for those who know how to use it. And granted, it is a lower-level framework compared to Laravel. It's made for professional developers, who know how to handle it, and also who want to do what they want and not necessarily get stuck with what Nest gives them, which is the case with Laravel. In other words, if you find Laravel's offerings falling short at some point, you'll be wishing it were less opinionated, as wiring up other packages can get "hard" too. I'd venture to say Nest is more in line with Symfony and even then, Nest is still lower level.

You also have to understand these PHP frameworks have been around basically longer than NodeJS has been around. So, the level of advancements for "finished modules" you can grab and use "easily" is going to be smaller than with PHP frameworks.

And lastly, in the bigger picture, PHP is losing on popularity and NodeJS is gaining. I saw this happening a few years ago. Maybe you've noticed it too and that is what got you here too? I've moved on from PHP and have never looked back. One main reason is, PHP can't do GraphQL well (notice I said well, not that it can't do it) and Nest and the NodeJS ecosystem supports GraphQL beautifully. In fact, one has to ask why Facebook hasn't created at least a Hack version of GraphQL and continually support the JS version. 🤔 I bet it is because they run GraphQL via NodeJS on their servers as a gateway to their HHVM servers, simply because the share-nothing architecture of PHP just doesn't cut it for a gateway application GraphQL represents. I'd even bet money on that guess.

#

At any rate, you seem to be fairly young. My advice to you is, stop whining, bitching and moaning all the time.

There are two kinds of people in this world. Ducks and Eagles. Whining, bitching and moaning all the time is like being a duck that quacks all day. And ducks are usually on the ground or swimming in ponds, quacking, getting no where fast and basically eating mud all day. Whereas the rest of us eagles are flying high and seeing a much more beautiful and bigger picture.... and just getting shit done. Don't be a duck. Be an eagle!

quasi umbra
#

❤️

#

Typescript Hype doesn't really make sense. It makes no sense why everyone is into typescript at the moment! The hype is not really

grim geode
#

nothing learned. 🙄 😿

First warning. Stop posting useless posts.

And my last advice. Go and learn what the difference is between a fixed typed and dynamically typed programming language. Once you learn that, you'll understand why there is some much energy around TypeScript.