2
votes

I need some help. I create User entity by using typeorm and I want to overwrite toResponseObject at Customer class for change return attribute but when I write finished the error occurs as "Type '{ userName: string; firstName: string; lastName: string; email: string; address: Address; token: string; }' is missing the following properties from type 'User': password, hasPassword, toResponObject, comparePassword" I guess this means I should return all of User Property but I don't want to return all of the attributes. What should I do?

user.entity.ts

import {PrimaryColumn, Column, BeforeInsert} from "typeorm";
import * as bcrypt from 'bcryptjs'
import * as jwt from 'jsonwebtoken'

export abstract class User {

    @PrimaryColumn()
    userName: string;

    @Column()
    password: string;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    email: string;

    @BeforeInsert()
    async hasPassword(){
        this.password = await bcrypt.hash(this.password,10)
    }

    async toResponObject(showToken:boolean = true){
        const {userName,firstName,lastName,email,token} = this
        const responseObject = {userName,firstName,lastName,email,token}
        if(showToken){
            responseObject.token = token
        }
        return responseObject
    }

    async comparePassword(attemp:string){
        return await bcrypt.compare(attemp,this.password)
    }

    protected get token(){
        const {userName,password} = this
        return jwt.sign({userName,password},process.env.SECRETKEY,{expiresIn:'7d'})
    }
}

customer.entity.ts

import { Pet } from "../pet/pet.entity";
import { Address } from "../address/address.entity";
import { Order } from "../order/order.entity";
import { Feedback } from "../feedback/feedback.entity";
import { User } from "../user/user.entity";
import { Entity, Column, ManyToOne, OneToMany } from "typeorm";

@Entity()
export class Customer extends User {

    @Column()
    phoneNumber: string;

    @OneToMany(type => Pet,pet => pet.owner)
    pets: Pet[];

    @ManyToOne(type => Address)
    address: Address;

    @OneToMany(type => Order,order => order.customer)
    orders: Order[];

    @OneToMany(type => Feedback,feedbacks => feedbacks.customer)
    feedbacks: Feedback[];

    async toResponObject(showToken:boolean = true):Promise<User>{
        const {userName,firstName,lastName,email,address,token} = this
        const responseObject = {userName,firstName,lastName,email,address,token}
        if(showToken){
            responseObject.token = token
        }
        return responseObject  // error ocuurs at this line
    }

}
1
thank you for your suggestion :D this solution is not working for me because I figure out this problem is from types of object that I define to return in the method. I tried to define a new object that contains attributes that I want to return and It's work :D but you give me the knowledge that I never know from typeORM. Thank you very muchS T Y X K
Cheers mate 👍!!hazardous

1 Answers

1
votes

Along with hazardous's comment, you could just return a Partial of the User using

async toResponObject(showToken:boolean = true):Promise<Partial<User>> {
  //...
}

This way you wouldn't ave to define a new object and can still get the desired output.