【发布时间】:2021-11-16 15:12:43
【问题描述】:
我在数据库中有一个用户集合,我想检索具有特定用户名的用户 我已经写了这个方法,但这会返回所有用户
findByUsername(username: string) {
return this.userModel.find({
'username' : username})
}
为什么这个查询不起作用 控制器
@Get('find/:username')
getUserById(@Param("username") username : string) : any {
console.log(username);
return this.usersService.findByUsername(username);
}
这是我的用户实体
从“@nestjs/mongoose”导入 { Schema, SchemaFactory }; 从“@nestjs/swagger”导入 { ApiProperty };
导出类型 UserDocument = User & Document;
@Schema()
export class User {
@ApiProperty()
id: string;
@ApiProperty()
username: string;
@ApiProperty()
email : string
@ApiProperty()
password: string;
}
export const UserSchema = SchemaFactory.createForClass(User);
这是服务
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { use } from "passport";
import {User,UserDocument} from '../users/entities/user.entity'
// This should be a real class/interface representing a user entity
@Injectable()
export class UsersService {
constructor(
@InjectModel(User.name) private readonly userModel : Model<User> )
{}
findById(userId: string) {
}
findByUsername(username: string) {
return this.userModel.find({"username": username}).exec();
}
【问题讨论】:
标签: javascript mongoose nestjs