gpt4 book ai didi

typescript - 类型缺少以下属性

转载 作者:行者123 更新时间:2023-12-04 17:38:05 26 4
gpt4 key购买 nike

我需要一些帮助。我使用 typeorm 创建用户实体,我想覆盖 toResponseObject
在 Customer 类中更改返回属性,但是当我写完时,错误发生为“类型 '{ userName: string; firstName: string; lastName: string; email: string; address: Address; token: string; }' 缺少以下内容'User' 类型的属性:密码、hasPassword、toResponObject、comparePassword” 我想这意味着我应该返回所有用户属性,但我不想返回所有属性。我该怎么办?

用户实体.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'})
}
}

客户.实体.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
}

}

最佳答案

随着危险的评论,你可以只返回用户的一部分使用

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

这样您就不必定义新对象并且仍然可以获得所需的输出。

关于typescript - 类型缺少以下属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55790897/

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