Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
TypeORM (+express) .findOne promise isn't working

Hello I hope your all well,

I'm encountering one annoying issue when using typeORM. I can't use .then().catch(); right after 'userRepo.findOne'. But I'm almost certain that .findOne generates a Promise...

Could you please help me?

Thanks in advance for your help.

Find below my User Entity:

import {Entity, PrimaryGeneratedColumn, Column, OneToMany} from "typeorm";
import { Post } from "./Post";

@Entity( {name : "Table_Personnes"} )
export class User {

    @PrimaryGeneratedColumn("uuid")
    Person_ID: number;

    @Column({ type: "varchar", length: 80})
    Person_Email: string;

    @Column()
    Person_Login: string;

    @Column()
    Person_Password: string;

    @Column()
    Person_Picture: string;

    @OneToMany( (type) => Post, (Post) => Post.User)
    Table_Post: Promise<Post[]>;
}

Find below my User Controller:

import {getRepository} from "typeorm";
import {NextFunction, Request, Response} from "express";

//Import bcrypt
import {bcrypt} from 'bcrypt'

//Import token library
import {jwt} from 'jsonwebtoken'

//Import the entity model
import {User} from "../entity/User";
import {Post} from "../entity/Post";

exports.login = (req, res, next) => {
    const userRepo = getRepository(User);
    userRepo.findOne({ Person_Email: req.body.user_email.toString().toLowerCase() });
    .then((user) => {
        console.log("User that logged in: ", user);
        if (!user) {
            return res.status(401).json({ error: 'User not found !' });
        }
        bcrypt.compare(req.body.password, user.Person_Password)
        .then(valid => {
            if (!valid) {
              return res.status(401).json({ error: 'wrong password !' });
            }
            res.status(200).json({
              userId: user._id,
              token: jwt.sign(
                { userId: user._id },
                'RANDOM_TOKEN_SECRET',
                { expiresIn: '24h' }
              )
            });
        })
        .catch(error => res.status(500).json({ error }));
    })
    .catch(error => res.status(500).json({ error }));
};
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda