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 }));
};