Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

A way to get raw base class? #158

Closed
TimTCrouch opened this issue May 30, 2018 · 2 comments
Closed

A way to get raw base class? #158

TimTCrouch opened this issue May 30, 2018 · 2 comments

Comments

@TimTCrouch
Copy link

Maybe I am not doing something right as I just started with Typegoose. I created a TG class that extends Typegoose as normal. Now I want to be able to bring this class into my Angular 6 client-side to simply use as a regular class to hold data sent by the server. However, when I do Mongoose immediately throws a mountain of errors. Is there a way to get the raw, class I created for my TG entity so I can use it on the client side? Or am I possibly doing something wrong? Not sure if it is useful but here is some of the code:

`import { prop, Typegoose } from "typegoose";

export class Door extends Typegoose {
@prop({index: true, unique: true, required: true})
id: number;

@prop({required: true})
open: boolean;

@prop({required: true})
locked: boolean;

//lock patterns are strings that when used on the matching key will open the lock
@prop()
lockPattern?: string;

//script to run when door is opened, not saved in db
openScript: any;

}`

And here is this same entity being created in an Angular front-end:
`import { Injectable } from '@angular/core';
import {Door} from "../../../../../../models/rooms/doorEntity";

@Injectable({
providedIn: 'root'
})
export class DoorService {
private door: Door = new Door();
}`

That is all that is required to cause the error. I am assuming that the Typegoose base automatically attempts to kick-off a Mongoose operation and of course, being on the client it isn't there and throws an error.

@hasezoey
Copy link
Contributor

typegoose/src/utils.ts

Lines 81 to 88 in ea1c0ec

/**
* Get the Class for a given Document
* @param document The Document
*/
export function getClassForDocument(document: mongoose.Document): any {
const modelName = (document.constructor as mongoose.Model<typeof document>).modelName;
return constructors[modelName];
}

this should work, but it is not supported to use typegoose without mongoose / client side

@hasezoey
Copy link
Contributor

hasezoey commented Sep 12, 2019

@Ben305 this can be closed as "fixed" & "stale"

@Ben305 Ben305 closed this as completed Sep 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants