'Nestjs Mongodb always return empty array
I am trying to create a simple nestjs project that connects to mongodb. I followed the official documentation of nestjs, but connected to my own db with "user" collection. When I tried to call the endpoint to findAll user, the result is always empty, even though the db contains records.
Any idea on what is causing the issue?
// user.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
export type UserDocument = User & Document;
export class Credential {
@Prop()
username: string;
@Prop()
password: string;
}
export class NameDetails {
@Prop()
firstName: string;
@Prop()
lastName: string;
}
export class ContactDetails {
@Prop()
email: string;
@Prop()
phoneNumber: string;
}
@Schema()
export class User extends Document {
@Prop()
_id: string;
@Prop()
credential: Credential;
@Prop()
nameDetails: NameDetails;
@Prop()
contactDetails: ContactDetails;
}
export const UserSchema = SchemaFactory.createForClass(User);
// users.module.ts
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { User } from './entities/user.entity';
import { UserSchema } from 'src/schemas/user.schema';
@Module({
imports: [MongooseModule.forFeature([{ name: User.name, schema: UserSchema }])],
controllers: [UsersController],
providers: [UsersService]
})
export class UsersModule {}
// users.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from 'src/schemas/user.schema';
@Injectable()
export class UsersService {
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}
async findAll(): Promise<User[]> {
return await this.userModel.find().exec();
}
}
Solution 1:[1]
In my case the problem was that I was not providing the DB name in the app.module.ts I only passed the URI to the mongodb cluster (not sure about the naming, please correct me if I am wrong), and every time you run the server it created a 'test' db on the cluster (with empty collection thus empty array), you can see that in the MongoDB Compass. The nestJS docs does not provide the DB name either.
Solution 2:[2]
If your schema class name is User
, then the actual collection name should be users
, rather than user
. Mongoose will attach an 's' with the class name after lowering the characters.
Solution 3:[3]
first of all you forget the Schema in your schema model :
@Schema()
export class Credential {}
secondly in your module you must put the same name of schema in your mongoose feature like this :
@Module({
imports: [MongooseModule.forFeature([{ name: Credential.name, schema: UserSchema }])],
controllers: [UsersController],
providers: [UsersService]
})
it must now work fine
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mrdjan Stajic |
Solution 2 | Sarath Radhakrishnan |
Solution 3 | safoine touil |