Я пытаюсь получить токен с требованием почтальона и ошибками ...
TypeError: Невозможно прочитать свойство 'findOne' из неопределенного
на User.getByEmail (сервер / модули / пользователь / service.ts: 47: 23)
в TokenRoutes.auth (сервер / модули / аутентификация / аутентификация: 18: 8)
Вот код:
controller.ts
import { Request, Response } from 'express';
import * as HTTPStatus from 'http-status';
import * as _ from 'lodash';
import { onError } from '../../api/responses/errorHandler';
import { onSuccess } from '../../api/responses/successHandler';
import { dbErrorHandler } from '../../config/dbErrorHandler';
import User from './service';
class UserController {
constructor() {}
getAll(req: Request, res: Response){
User
.getAll()
.then(_.partial(onSuccess, res))
.catch(_.partial(onError, res, `Erro ao buscar todos os usuários`))
}
createUser(req: Request, res: Response){
User
.create(req.body)
.then(_.partial(onSuccess, res))
.catch(_.partial(dbErrorHandler, res))
.catch(_.partial(onError, res, `Erro ao inserir novo usuário`))
}
getById(req: Request, res: Response){
const userId = parseInt(req.params.id);
User.getById(userId)
.then(_.partial(onSuccess, res))
.catch(_.partial(onError, res, `Usuário não encontrado`))
}
updateUser(req: Request, res: Response){
const userId = parseInt(req.params.id);
const props = req.body;
User.update(userId, props)
.then(_.partial(onSuccess, res))
.catch(_.partial(onError, res, `Erro ao atualizar usuário`))
}
deleteUser(req: Request, res: Response){
const userId = parseInt(req.params.id);
User.delete(userId)
.then(_.partial(onSuccess, res))
.catch(_.partial(onError, res, `Erro ao deletar usuário`))
}
}
export default new UserController();
routes.ts
import { Request, Response } from 'express';
import UserController from './controller';
class UserRoutes {
constructor(){}
index(req: Request, res: Response){
return UserController.getAll(req, res);
}
create(req: Request, res: Response){
return UserController.createUser(req, res);
}
findOne(req: Request, res: Response){
return UserController.getById(req, res);
}
update(req: Request, res: Response){
return UserController.updateUser(req, res);
}
destroy(req: Request, res: Response){
return UserController.deleteUser(req, res);
}
}
export default new UserRoutes();
service.ts
import { IUser, IUserDetail, createUser, createUsers, createUserById, createUserByEmail } from './interface';
import * as Bluebird from 'bluebird';
const model = require('../../models');
class User implements IUser {
public id: number;
public nm_usuario: string;
public nm_email_usuario: string;
public cd_senha_usuario: string;
public nm_tipo_usuario: string;
public cd_cnpj_usuario: number;
public cd_cpf_usuario: number;
public nm_razao_social_usuario: string;
public nm_sexo_usuario: string;
public cd_cep_usuario: number;
public nm_estado_usuario: string;
public dt_nascimento_usuario: Date;
public nm_cidade_usuario: string;
public cd_telefone_usuario: number;
public cd_ip_usuario: string;
public nm_endereco_usuario: string;
public cd_numero_endereco_usuario: number;
public ds_complemento_endereco_usuario: string;
public ds_foto_usuario: string;
constructor(){}
create(user: any){
return model.User.create(user);
}
getAll(): Bluebird<IUser[]>{
return model.User.findAll({
order: ['nm_usuario']
})
.then(createUsers);
}
getById(id: number): Bluebird<IUserDetail>{
return model.User.findOne({
where: { id }
})
.then(createUserById);
}
getByEmail(nm_email_usuario: string): Bluebird<IUserDetail>{
return model.User.findOne({
where: { nm_email_usuario }
})
.then(createUserByEmail);
}
update(id: number, user: any){
return model.User.update(user, {
where: { id },
fields: ['nm_usuario', 'nm_email_usuario','cd_senha_usuario','nm_tipo_usuario','cd_cnpj_usuario','cd_cpf_usuario','nm_razao_social_usuario','nm_sexo_usuario','cd_cep_usuario','nm_estado_usuario','dt_nascimento_usuario','nm_cidade_usuario','cd_telefone_usuario','cd_ip_usuario','nm_endereco_usuario','cd_numero_endereco_usuario','ds_complemento_endereco_usuario','ds_foto_usuario'],
hooks: true,
individualHooks: true
});
}
delete(id: number){
return model.User.destroy({
where: { id }
});
}
}
export default new User();
auth.ts
import { Request, Response } from 'express';
import * as _ from 'lodash';
import User from '../User/service';
import authSuccess from '../../api/responses/authSuccess';
import authFail from '../../api/responses/authFail';
class TokenRoutes {
auth(req: Request, res: Response){
const credentials = {
nm_email_usuario: req.body.nm_email_usuario,
cd_senha_usuario: req.body.cd_senha_usuario
};
if(credentials.hasOwnProperty('nm_email_usuario') && credentials.hasOwnProperty('cd_senha_usuario')){
User
.getByEmail(credentials.nm_email_usuario)
.then(_.partial(authSuccess, res, credentials))
.catch(_.partial(authFail, req, res));
}
}
}
export default new TokenRoutes();
Итак ... почтальон дал мне ошибку 500, вероятно, потому что не может выполнить метод findOne.