tsedio/tsed-example-mongoose

Mongoose Schema Types @Ref()

Closed this issue · 5 comments

Hi @Romakita , I am migrating my project from js to Ts.ED
Issue in using Mongoose Schema Types@Ref()
Workspace.ts

import { Required } from "@tsed/common";
import { Model, Ref, ObjectID } from "@tsed/mongoose";
import { Workspace } from "../workspace/Workspace";

@Model()
export class User {
    @ObjectID("id")
    _id: string;

    @Required()
    name: string;

    @Ref(Workspace)
    workspace: Ref<Workspace>;
}

User.ts

import { Required} from "@tsed/common";
import {Model, Ref, ObjectID} from "@tsed/mongoose";
import { User } from "../users/User";
@Model()
export class Workspace {
  @ObjectID("id")
  _id: string;

  @Required()
  name: string;

  @Ref(User)
  createdBy: Ref<User>;
}

Injecting User Model in service.
UserService.ts

import { Inject, Service } from "@tsed/common";
import { MongooseModel } from "@tsed/mongoose";
import { User } from "../../models/users/User";

@Service()
export class UserService {
  @Inject(User)
  private User: MongooseModel<User>;

  async find(id: string): Promise<User> {
    const user = await this.User.findById(id).exec();
    return user;
  }

}

Getting below error.

[2020-07-06T17:31:48.467] [ERROR] [TSED] - TypeError: Cannot read property 'prototype' of undefined
    at Object.getClass (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\core\src\utils\ObjectUtils.ts:41:17)
    at Function.hasOwn (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\core\src\class\Metadata.ts:319:40)
    at Store._storeGet (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\core\src\class\Store.ts:226:21)
    at Store._map (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\core\src\class\Store.ts:34:23)
    at new Store (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\core\src\class\Store.ts:36:7)
    at Function.from (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\core\src\class\Store.ts:53:12)
    at Object.Ref (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\@tsed\mongoose\src\decorators\ref.ts:51:54)
    at Object.<anonymous> (C:\Users\..\Downloads\tsed-example-mongoose\src\models\workspace\Workspace.ts:12:4)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module.m._compile (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\ts-node\src\index.ts:814:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\..\Downloads\tsed-example-mongoose\node_modules\ts-node\src\index.ts:817:12)  
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)

Code: Nareshtekula@3a1db48
Thanks

Hello @Nareshtekula
I added some comment to your github repository :)

See you

Hi @Romakita
Same Error.
[2020-07-09T17:56:41.735] [ERROR] [TSED] - TypeError: Cannot read property 'prototype' of undefined

Issue is when User Model has @Ref (Workspace ) and Workspace Model has @Ref(User).

It works if i remove @Ref() from any one model.

Thank you.

Ok I'll try to reproduce your bug :)

Ok I missed, you have a circular reference :)

So to avoid this, try this example:

import { Required } from "@tsed/common";
import { Model, Ref, ObjectID } from "@tsed/mongoose";
import { Workspace } from "../workspace/Workspace";

@Model()
export class User {
    @ObjectID("id")
    _id: string;

    @Required()
    name: string;

    @Ref("Workspace") // add quote. The Workspace ref will be resolved later
    workspace: Ref<Workspace>;
}

See you
Romain

Got it. Thank you.