-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange behaviour when referencing 'model.modelName' #184
Comments
simple: PS: i edited your issue to have code-highlighting |
Neither solution works. |
A comment on the sidelines (correct me if I'm wrong): Unless github's policies have changed, a issue can only be reopened if it was yourself who closed it. Otherwise, only a repo collaborator can. |
@LucioMF then just ask to reopen, dont create an new issue that is exactly the same |
@LucioMF i just tested your provided code (without nest) and it works, no problem at all (the static could you add the following lines before your find query (user service findAll): console.log("default connection", mongoose.connection.readyState);
console.log("all other", mongoose.connections.map(x => x.readyState)); here the code: // NodeJS: 12.14.1
// MongoDB: 4.2-bionic (Docker)
import { getModelForClass, prop, modelOptions, ReturnModelType } from "@typegoose/typegoose"; // @typegoose/typegoose@6.2.2
import * as mongoose from "mongoose"; // mongoose@5.8.9
@modelOptions({
schemaOptions: {
toJSON: {
virtuals: true,
getters: true,
},
toObject: {
virtuals: true,
getters: true,
},
timestamps: true
},
})
export class BaseModel<T> {
public createdAt?: Date;
public updatedAt?: Date;
public id?: string;
}
export class User extends BaseModel<User> {
@prop({ lowercase: true, trim: true, required: [true, "username is required"], minlength: [1, "Must be at least 4 characters"] })
public username!: string;
@prop({ required: [true, "password is required"], minlength: [1, "Must be at least 6 characters"] })
public password!: string;
public static get model(): ReturnModelType<typeof User> {
/* old version way */
// return new User().getModelForClass(User);
return getModelForClass(User);
}
public static get modelName(): string {
// this function never got called
console.log("p s g modelName");
return this.model.modelName;
}
}
export const UserModel = getModelForClass(User);
(async () => {
await mongoose.connect(`mongodb://localhost:27017/`, { useNewUrlParser: true, dbName: "verify186", useCreateIndex: true, useUnifiedTopology: true });
const user = await UserModel.create({ username: "user1", password: "somepwd" });
console.log(user);
const found = await UserModel.findById(user._id).exec();
console.log(found);
const foundAll = await UserModel.find({}).exec();
console.log("foundall", foundAll);
await mongoose.disconnect();
})(); the only thing i changed is to use the mongoose |
I don't see you call the static "modelName" in question. For example: import {
Controller,
Get,
} from '@nestjs/common';
import { UserService } from './user.service';
import { ApiTags } from '@nestjs/swagger';
import { User } from './models/user.model';
@Controller('user')
@ApiTags(User.modelName) /* Here I use it */
export class UserController {
constructor(
private readonly userService: UserService,
) { } |
@LucioMF sorry, i never used swagger and know little about nestjs & graphql, but i tested logging this too ( Update: i renamed the function and noticed it is an static getter, which isnt supported by mongoose (just gets ignored), so either use an static function or some non-static getter |
@LucioMF i would recommend opening an mongoose issue with an use case (i cant, because i dont know an use case that would need static getter & setters) |
Both "model" and "modelName" are static because they are class properties, I don't need Mongoose to recognize them as model properties. |
@LucioMF why? update: i tried it, still no problem |
But the function is triggered, right? I tried calling the function ONLY for a "console.log" and certainly no problem occurs. Here you have a simple configuration to use Swagger: import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const hostDomain = 'http://localhost:8080';
const swaggerOptions = new DocumentBuilder()
.setTitle('titler')
.setDescription('some description')
.setVersion('1.0.0')
.addTag('API')
.build();
const swaggerDoc = SwaggerModule.createDocument(app, swaggerOptions);
app.use('/api/docs/swagger.json', (req, res) => {
res.send(swaggerDoc);
});
SwaggerModule.setup('api/docs', app, null, {
swaggerUrl: `${hostDomain}/api/docs/swagger.json`,
explorer: true,
swaggerOptions: {
docExpansion: 'list',
filter: true,
showRequestDuration: true,
},
});
app.enableCors();
await app.listen(8080);
}
bootstrap(); Then you will be able to use Swagger's decorators in your controller and could see the result in localhost:8080/api/docs |
@LucioMF could you make an repo so we have the same code and versions? |
Sure: https://github.com/LucioMF/typegoose-swagger pre-requirements: Instructions:
|
result of: console.log("default connection", mongoose.connection.readyState);
console.log("all other", mongoose.connections.map(x => x.readyState)); is
which means you have the problem of kpfromer/nestjs-typegoose#60 with further reading and trying to fix it would result in kpfromer/nestjs-typegoose#57 |
TL;DR: the connection made with TypegooseModule.forRoot is an non-default connection, and the model is associated with the default connection, which never gets connected so all queries stall and wait to be connected to the database (and naming connections with |
Sorry, I still do not understand why queries only get stall when you want to access the "modelName" specifically (because if accessed from another side there is no problem) from Swagger decorators. |
no there isnt (at least that i know of), because i still dont know much about nestjs and the nestjs-typegoose codebase i cant really help and kpfromer (the owner of nestjs-typegoose) is rather slow to respond |
Thanks. |
if you want to name the class something like maybe try the |
Thanks. import { getName } from '@typegoose/typegoose/lib/internal/utils';
static get modelName(): string {
// return this.model.modelName; /*doesn't work*/
// return 'User'; /*dirty workaround*/
return getName(User); /*nice workaround*/
} |
Versions
What is the Problem?
I recently upgrade to version 6
I was using the "modelName" property of the model, to tag my endpoints in Swagger. But with the new version this seems to somehow interfere with the queries (of any kind) to the database. They stay frozen and never respond
Code Example
Base Model:
User Model:
User service:
User Controller:
Steps to reproduce
Run the code above (or some similar) and call ''listusers" method. It will get frozen in the findAll query.
Now replace the line
return this.model.modelName;
in the virtual get property of the model byreturn 'User';
and it will work.Do you know why it happenes?
no
The text was updated successfully, but these errors were encountered: