George Tarida
George Tarida

Reputation: 141

Mongoose find does not return results mongo does

I have a method called getTeamEmployees which should get employees that have teamId equals to the method parameter teamId. It looks like this:

public getTeamEmployees(teamId: number): Promise<any> {
    return MDBDatabaseController.EmployeeModel.find({ teamId: teamId }).exec();
}

I have enabled mongoose debug and in the console I can see what query mongoose execute. This is the query:

employees.find({ teamId: 13 }, { fields: {} })

If I execute this using the mongo shell I get this:

    db.employees.find({ teamId: 13 }, { fields: {} })
Error: error: {
    "ok" : 0,
    "errmsg" : ">1 field in obj: {}",
    "code" : 2,
    "codeName" : "BadValue"
}

And if I execute it without the { fields: {} } part everything is ok. It gives me 5 results.

I'm using mongoose with typescript and my model is instantiated like this:

MDBDatabaseController.EmployeeModel = model<IEmployee>('Employee', EmployeeSchema.schema);

Where IEmployee is an interface that extends mongoose.Document and has a field called teamId that is of type Number capital N.

EmployeeSchema is a class that has a static property called 'schema' that is equal to a new instance of mongoose.Schema which has a property called teamId of type Number.

What could be the problem ?

Upvotes: 0

Views: 797

Answers (1)

George Tarida
George Tarida

Reputation: 141

The problem was that I was doing something wrong in the resolve function of the promise. I was doing something like res.status(200).send({}) . So no matter what query was executed all that I was getting back on the client side was {}.

Upvotes: 0

Related Questions