gpt4 book ai didi

javascript - 无法使用 Nodejs 和 Sequelize 读取未定义的属性 'findOne'

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:56 25 4
gpt4 key购买 nike

我尝试通过 postman 请求获取 token ,但发生了错误...

TypeError: Cannot read property 'findOne' of undefined at User.getByEmail (server/modules/User/service.ts:47:23) at TokenRoutes.auth (server/modules/Auth/auth.ts: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();

所以... postman 给我一个错误 500,可能是因为无法执行 findOne 方法。

最佳答案

问题被定义为返回是Usuario in userModel

我调用model.User

所以我称之为...调用 model.Usuario 并工作

关于javascript - 无法使用 Nodejs 和 Sequelize 读取未定义的属性 'findOne',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com