Reputation: 167
I'm working on a project with NestJs and TypeORM with MongoDB. Earlier we were working with express and mongoose and to find results in an array column we were using something like this :
I'm working on creating a REST API using nestJS + TypeORM and MongoDB. The problem I am having is whenever I make a GET call to findOne or find all items in an entity/table it results in an error either saying Cannot read property 'prototype' of undefined or TypeError: Right-hand side of 'instanceof' is not an object.
My Entity
@Entity({ name: 'discordconfigs' })
export class DiscordConfig extends BaseEntity {
@ObjectIdColumn()
_id: ObjectID;
@Column()
clientId: string;
}
DI for the REPO (Inside Constructor in service file)
@InjectRepository(DiscordConfig)
private guildConfigRepository: MongoRepository<DiscordConfig>,
Usually, when using NestJS + TypeORM with an SQL database I can use this code snippet below to return all items in the array. Doing this with MongoDB results in an error Cannot read property 'prototype' of undefined and does not return anything.
return this.guildConfigRepository.find();
I can change the above to this and it will return the required data back to me but will result in the same error being thrown instead I think it is just a warning instead of an error.
return await this.guildConfigRepository
.createCursor(this.guildConfigRepository.find())
.toArray();
If I want to return a specific item then I have tried to use this but I get an error TypeError: Right-hand side of 'instanceof' is not an object. I console logged + checked the type of the parameter coming in and it was the correct data I inputted as a parameter and also a string.
return await this.guildConfigRepository.findOne({ clientId });
I am able to POST data and save it into the database without causing any errors. If I change my database type to an MYSQL database and call the repository using the normal TypeORM way then I get no errors.
How would I fix this using typeORM or would it be easier using mongoose. I was considering switching to mongoose however I wanted to get typeORM working since it works natively with nestJS.
Here is how I connect to the DB (Inside Imports in App.Module)
TypeOrmModule.forRoot({
type: 'mongodb',
url: 'mongoURI',
database: 'database',
entities: entities,
synchronize: true,
}),
if I switch it to a MYSQL database with connection settings like this my API works without any errors.
TypeOrmModule.forRoot({
type: 'mysql',
host: 'ipaddress',
port: 3306,
username: 'userLogin',
password: 'userPassword',
database: 'databaseName',
entities: entities,
synchronize: true,
}),
Upvotes: 3
Views: 6657
Reputation: 77
You can use Mongoose instead of TypeORM, you just need to change some configuring in some files. The Mongoose integration with MongoDB is quite to easy to understand. This is the documentation link to configure and use: https://docs.nestjs.com/recipes/mongodb#mongodb-mongoose
Upvotes: -2