获取类型 '{ username: string; password: string; }' 的错误参数不能分配给类型 'RequiredEntityData<User>' 的参数



我的项目工作良好,但我一直得到红色下划线在这行代码:

{
username: options.username,
password: hasedPassword
}

当我将它悬停时,它显示:

类型为'{username: string;密码:字符串;}不是可赋值给'RequiredEntityData'类型的参数。类型"{用户名:字符串;密码:字符串;}'缺少以下内容属性从类型'{createdAt: EntityDataItem;updatedAt:EntityDataItem;用户名:EntityDataItem;密码:EntityDataItem;}': createdAt, updatedats (2345)

这是整个文件:

import { User } from "../entities/User";
import { MyContext } from "../types";
import { Arg, Ctx, Field, InputType, Mutation, Resolver } from "type-graphql";
import argon2 from 'argon2';
@InputType()
class UsernamePasswordInput {
@Field()
username: string
@Field()
password: string
}
@Resolver()
export class UserResolver {
@Mutation(() => User) 
async register(
@Arg('options') options: UsernamePasswordInput,
@Ctx() {em}: MyContext
) {
const hasedPassword = await argon2.hash(options.password)
const user = em.create(User, {
username: options.username,
password: hasedPassword
})
await em.persistAndFlush(user)
return user
}
}

package.json:

{
"name": "practicegraphQL",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/index.js",
"start": "ts-node src/index.ts",
"create:migration": "mikro-orm migration:create"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.35",
"nodemon": "^2.0.16",
"ts-node": "^10.8.0",
"typescript": "^4.6.4"
},
"dependencies": {
"@mikro-orm/cli": "^5.1.4",
"@mikro-orm/core": "^5.1.4",
"@mikro-orm/migrations": "^5.1.4",
"@mikro-orm/postgresql": "^5.1.4",
"apollo-server-express": "^2.25.3",
"argon2": "^0.28.5",
"express": "^4.18.1",
"graphql": "15.7.2",
"pg": "^8.7.3",
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.1.1"
},
"mikro-orm": {
"useTsNode": true,
"configPaths": [
"./src/mikro-orm.config.ts",
"./dist/mikro-orm.config.js"
]
}
}

如何修复这个错误?

已更新,添加用户实体:

@ObjectType()
@Entity()
export class User {
@Field()
@PrimaryKey()
_id!: number;
@Field(() => String)
@Property()
createdAt: Date = new Date();
@Field(() => String)
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@Field(() => String)
@Property({unique: true})
username!: string;

@Property()
password!: string;
}

@Resolver

@Resolver()
export class UserResolver {
@Mutation(() => User) 
async register(
@Arg('options') options: UsernamePasswordInput,
@Ctx() {em}: MyContext
) {
const hasedPassword = await argon2.hash(options.password)
const user = em.create(User, {
username: options.username,
password: hasedPassword
// add these here 
createdAt: new Date,
updatedAt: new Date
})
await em.persistAndFlush(user)
return user
}
}

只是添加添加createdAt: new Date, updatedAt: new Date{username: options.username,password: hasedPassword}

这样的{username: options.username,password: hasedPassword, createdAt: new Date, updatedAt: new Date }文本编辑器不会抛出错误。

虽然会抛出错误,但您的代码将正常工作,并且您可以向数据库添加行

相关内容

最新更新