Coder Social home page Coder Social logo

Stuck on findOne about typegoose HOT 13 CLOSED

szokodiakos avatar szokodiakos commented on July 20, 2024 5
Stuck on findOne

from typegoose.

Comments (13)

Christilut avatar Christilut commented on July 20, 2024 3

Nice work! Removing node_modules/typegoose/node_modules/mongoose fixed it for me (as a quick test).

And your above workaround works too.

from typegoose.

vinczedani avatar vinczedani commented on July 20, 2024 2

Hi @valneras

You should pass your own mongoose instance in the getModelForClass method if you have any. The second parameter (options) has a property called existingMongoose. This should make the typegooses owned fallback mongoose unused, so the deprecation warning should disappear.

You can also pass in an existing mongoose connection if you have any. It can also passed down to typegoose in the getModelForClass methods second parameter. Under the existingConnection key in the options object.

The precedency of all the mongooses inside typegoose is the following:

  1. existingConnection
  2. existingMongoose
  3. typegooses own mongoose

Unfortunately, we don't really have spare time for typegoose at the moment. In the future we plan to deprecate our own mongoose as a dependency and add it as a peer dependency instead. But since it is a breaking change for those who rely on the built in mongoose instance, it is more complicated to do, than just changing two lines in the package.json.
I hope this will fix your problem.

Cheers,
Daniel

from typegoose.

iamdanthedev avatar iamdanthedev commented on July 20, 2024 1

This is a workaround, it prevents installing non-flat mongoose dependency

yarn remove typegoose mongoose
yarn add [email protected] typegoose

from typegoose.

vinczedani avatar vinczedani commented on July 20, 2024 1

Hi @Christilut
We are aware of the issue that typegoose has its own mongoose version. It is on our todo list to list mongoose as a peer dependency instead of a normal one. It should resolve some issues.

The pre save is after the validation, but mongoose offers pre validation hook too. In your case, you should give it a try ;)

from typegoose.

Christilut avatar Christilut commented on July 20, 2024

Probably a mongoose issue but I really can't figure this one out.

Tried bluebird, older mongoose versions, lots of different settings but nothing seems to work.

from typegoose.

iamdanthedev avatar iamdanthedev commented on July 20, 2024

Same story! If I switch to native mongoose schema way - everything works :-(

from typegoose.

iamdanthedev avatar iamdanthedev commented on July 20, 2024

Tried to supply 'existingCollection' to models - doesn't help

from typegoose.

Christilut avatar Christilut commented on July 20, 2024

I'm using typegoose to create a boilerplate so anyone that wants to take look: here it is

Use the typegoose-hangs branch.

Copy .env.example to .env, (change MONGO_HOST if needed) run npm install, then npm run dev and it should work (I hope 😛)

This is the last I see:

saving new user for testing
pre save !!!
(node:1685) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

Check /node-api-boilerplate/server/models/user/model.ts for relevant model code and config/mongoose.ts for mongoose init.

from typegoose.

iamdanthedev avatar iamdanthedev commented on July 20, 2024

The problem is that typegoose has mongoose (and typescript) defined as dependencies in package.json. And versions are set strict. That's why typegoose actually brings his own mongoose and typescript inside node_modules/typegoose/node_modules , and uses not that mongoose which we require and install separately

This deps should be peerDependencies or maybe the version should not be set strict

:-(

from typegoose.

Christilut avatar Christilut commented on July 20, 2024

Just noticed that my pre-save stopped working after the above workaround

@pre<User>('save', async function (next) {
  console.log('pre save !!!') 

  next()
})

export class User extends Typegoose {
}

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

This is pretty much the example code (and it did work earlier).

from typegoose.

Christilut avatar Christilut commented on July 20, 2024

My pre-save suddenly started working again, this is what happenend:

In my model I had the created property, which is to be set in the pre-save

  @prop({
    required: true
  })
  created: Date

Pre-save:

  if (this.isNew) {
    this.created = new Date()
  }

But that triggered a validation error that created was required. So apparently validation is triggered before the pre-save and properties that get set in the pre-save shouldn't have required on them.

from typegoose.

neckaros avatar neckaros commented on July 20, 2024

very hard to find issue indeed! my await was just never returning and nerver throwing!

from typegoose.

hasezoey avatar hasezoey commented on July 20, 2024

this can be closed, because mongoose 5.0+ uses the default Promise, and typegoose uses an "up-to-date" mongoose version

@Ben305

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.