Coder Social home page Coder Social logo

Comments (6)

vinczedani avatar vinczedani commented on July 20, 2024 2

Hi @dekaha1

Thanks for using typegoose and reporting your issue. I currently have no time to reproduce your issue. But I think I might know what is going wrong.

First You don't pass down your existing mongoose to Typegoose. You should do it like the following:

getModelForClass(User, { existingMongoose: mongoose })

The other thing is, that you use the ref as 'UserModel'. I think it wont work this way. typegoose creates models based on the constructor name (in this case 'User'). So you should try the following inside your bus Model description:

driver: { type: Schema.Types.ObjectId, ref: 'User' }, 

I hope you will be able to resolve your issues based on these comments.
May I ask, why are you using booth classic mongoose and typegoose at the same time?

Cheers,
Daniel

from typegoose.

dekaha1 avatar dekaha1 commented on July 20, 2024

Sorry for the bad styling

Car Model does not extend Typegoose

import * as mongoose from 'mongoose'
const Model = mongoose.Schema
const CarModel = new Model (
  { color: String,
  driver: { type: Schema.Types.ObjectId, ref: 'UserModel' }, 
  })

UserModel extends Typegoose

export class User extends Typegoose {
@prop()
public name: string;
}

export const UserModel = new User().getModelForClass(User)

Different file to create Data to save

const autoMobile = {};
autoMobile.color = 'black';
autoMobile.driver = new UserModel({name: 'Bob'});
Car.create(autoMobile, (createdCar: any) => { return createdCar; })

Everything is saved correctly except the name attribute on the userModel. Mogoose even creates the ID correctly. Any help is much appreciated

from typegoose.

bitjson avatar bitjson commented on July 20, 2024

@dekaha1 thanks, { existingMongoose: mongoose } is what I needed too.

May I ask, why are you using booth classic mongoose and typegoose at the same time?

I'd love to see an example of how to avoid installing mongoose in my project completely – is that possible? I'm currently having trouble getting the example in readme.md working without the existingMongoose setting.

from typegoose.

hershal avatar hershal commented on July 20, 2024

Hey @vinczedani, your suggestion to add { existingMongoose: mongoose } fixed it for me too. Cheers!

from typegoose.

hasezoey avatar hasezoey commented on July 20, 2024

@bitjson

I'd love to see an example of how to avoid installing mongoose in my project completely – is that possible? I'm currently having trouble getting the example in readme.md working without the existingMongoose setting.

not possible, because as the name says its "typescript" + "mongoose" = "typegoose" so by design, it will not be supported without mongoose, typegoose is just a "wrapper" for schemas & models, not for the functions

Update:
in v6.0.0, mongoose is still a peer-dependencie, but could be get as import { mongoose } from "@hasezoey/typegoose"

@Ben305 this can be closed

from typegoose.

hasezoey avatar hasezoey commented on July 20, 2024

@Ben305 Bump to close

from typegoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.