routing-controllers
routing-controllers copied to clipboard
I cannot return value to the controller from the model
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
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
Any update on this. I am stuck too.
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.