routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

I cannot return value to the controller from the model

Open INGAngelSanchez opened this issue 6 years ago • 2 comments

This problem is for the author of this library or similarly for someone else who has had this problem and could solve What's the matter? The problem is that I can't return the model value to the controller In the next code I illustrate this problem more clearly : First Controller and after show the model `import {Controller, Param, Body, Get, Post, Put, Delete,ContentType,HttpCode} from "routing-controllers" //Request and Response import {Req,Res} from "routing-controllers" import {Request, Response} from "express" //Modelos import {Client,User} from '../models/export'

@Controller() export class UserController { private cliente:any constructor(){ this.client = new Client() }

@Post("/Sign-up")
signUp(@Req() request: Request, @Res() response: Response ):any{
  let user: User = request.body
  return this.client.signup(user)
}

}`

Model: `import {User} from './user.model' import {userSchema} from './sub-models/user' import bcrypt from 'bcrypt-nodejs' import {Jwt} from '../services/jwt' export class Cliente { private user:User[] private userSchema:any private jwt:any constructor(){ this.user = [] this.userSchema = new userSchema() this.jwt = new Jwt() } signup(user:User):any{
let userSchema:any = this.userSchema.userSchema() let user = new userSchema() user.email = user.email user.name = user.name user.surname = user.surname user.avatar = null userSchema.findOne({email:user.email.toLowerCase()},(err:any,issetUser:any)=>{ if(err){ console.log('Error:',err) } if(!issetUser){ let saltRounds : number = 10 bcrypt.genSalt(saltRounds,(err:any,result:any)=>{ if(err){ console.log('error') }else{ let salt = result bcrypt.hash(usuario.password,salt,null,(err:any,hash:any)=>{ user.password = hash

            user.save((err:any,userStored:any)=>{
               if(err)
               {
                 return 500
               }else
               {
               if(!userStored)
               {
                return 404  
               }
               else
               {        /****
                        **In this part return data from the database but never reach the controller   ****/**
                        return {user:userStored} 
               }
              }
            })
          })
        }
      })
   	}else{
   		return true
   	}
})

}

}` As we can see I'm using Moongose to make a query to the database, in this case is the method of logging. First valid that the user is not in the database, then use the library bcrypt to encrypt the password and then save the user to later return the result of the database But that is what happens that when returning the result does not recognize the controller and I mark the following error : Error at NotFoundError.HttpError [as constructor] (C:\Users\migue\Desktop\Curso de Angular 6 Tienda Eccomerce\tienda-eccomerce-backend\node_modules\routing-controllers\http-error\HttpError.js:27:23) at new NotFoundError (C:\Users\migue\Desktop\Curso de Angular 6 Tienda Eccomerce\tienda-eccomerce-backend\node_modules\routing-controllers\http-error\NotFoundError.js:20:28) at ExpressDriver.handleSuccess (C:\Users\migue\Desktop\Curso de Angular 6 Tienda Eccomerce\tienda-eccomerce-backend\node_modules\routing-controllers\driver\express\ExpressDriver.js:289:23) at C:\Users\migue\Desktop\Curso de Angular 6 Tienda Eccomerce\tienda-eccomerce-backend\node_modules\routing-controllers\RoutingControllers.js:128:61 at at process._tickCallback (internal/process/next_tick.js:188:7) What is the solution to this problem? Try different methods as follows: Creation of functions, class creation, global variables etc. But I can't find the solution please help In the same way I would appreciate if anyone could show me their projects created with this library and using MongoDB so I can guide me in my project I hope your answers

In this part return data from the database but never reach the controller Note The registration method is successful when registering but not when returning the value to the controller

INGAngelSanchez avatar Jul 04 '18 03:07 INGAngelSanchez

Any update on this. I am stuck too.

iamanthos avatar Jan 24 '20 10:01 iamanthos

The error returned from routing-controllers refers to NotFoundError, this is usually when the route is not resolved.

I might be not reading this correctly, however, one reason why the return value from the model is not reaching the controller before it returns to the client could be because the save method in the model returns a promise. You can try awaiting for the promise to resolve. This would potentially allow you to switch between and return the response.

Reference: https://mongoosejs.com/docs/async-await.html

  • Also a side note, the code and the error message is not formatted properly, which makes is a bit hard to read and understand the underling issue.

gayanhewa avatar Mar 04 '23 01:03 gayanhewa