import {
Column, ManyToOne, JoinColumn,
Entity, PrimaryGeneratedColumn
} from "typeorm";
import { User } from "./user";
@Entity({ name: "auths" })
export class Auth {
@PrimaryGeneratedColumn() readonly id!: number;
@Column() user_id!: string;
@Column() type!: string;
@Column() identifier!: string;
@Column() credential!: string;
@ManyToOne(() => User, { cascade: true })
@JoinColumn({ name: "user_id" }) user!: User;
}
import "reflect-metadata";
import { DataSource } from "typeorm";
import { ConnectionOpts } from "@/scripts/config";
import { User } from "./entity/user";
import { Auth } from "./entity/auth";
const entities = [User, Auth];
const options = { ...ConnectionOpts, entities };
const AppDataSource = new DataSource(options);
export default AppDataSource;
import AppDataSource from "@/database";
import { User } from "@/database/entity/user";
import { Auth } from "@/database/entity/auth";
import { tSession } from "@/scripts/types";
async function login(req: NextApiRequest, res: NextApiResponse) {
const { type, mobile, verify } = req.body;
const userRepository = AppDataSource.getRepository(User);
const authRepository = AppDataSource.getRepository(Auth);
const session: tSession = req.session;
if (session.verifyCode === verify) {
const condition = { type, identifier: mobile };
// 运行到这个查找语句,就报错:No metadata for "Auth" was found.
const authRes = await authRepository.findOneBy(condition);
}
}
查了下资料,说是通常因为实体没有正确的定义、导入或注册引起的。
但是我看了好几遍,没看出什么问题来。不知道怎么办,求老师指导。