user3135515
user3135515

Reputation: 141

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();
  }

}


Upvotes: 1

Views: 1849

Answers (4)

extractor
extractor

Reputation: 37

in the entity.ts, when adding @Schema() annotation, pls add the collection name.

example:

@Schema({ collection: 'users' })

Upvotes: 1

safoine touil
safoine touil

Reputation: 1

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

Upvotes: 0

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.

Upvotes: 0

Mrdjan Stajic
Mrdjan Stajic

Reputation: 11

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.

Upvotes: 1

Related Questions