Coder Social home page Coder Social logo

Comments (3)

pgsfredda avatar pgsfredda commented on July 20, 2024

Hi @vogong,
please, can you post an example?

from typegoose.

vogong avatar vogong commented on July 20, 2024
interface FindOrCreateResult<T> {
    created: boolean;
    doc: InstanceType<T>;
  }

@plugin(findOrCreate)
class User extends Typegoose {
    @prop()
    name?: string;

    // @staticMethod
    // static helloWorld(this: ModelType<User> & typeof User) {
    //   console.log("Hello WOrld");
    // }

    static findOrCreate: (condition: any) => Promise<FindOrCreateResult<User>>;
}
const model = new User().getModelForClass(User);

\\Mongoose connection and promise

connection.on('connected', () => {
    (async () => {
        var car = new model({ name: "[email protected]" });
        await car.save();
    })();
});

The above example fails to run with the following error unless you uncomment the static function and initialize statics

typegoosetest\node_modules\mongoose-findorcreate\index.js:8
schema.statics.findOrCreate = function findOrCreate(conditions, doc, options, callback) {
^

TypeError: Cannot set property 'findOrCreate' of undefined
at findOrCreatePlugin (typegoosetest\node_modules\mongoose-findorcreate\index.js:8:31)
at Schema.plugin (typegoosetest\node_modules\typegoose\node_modules\mongoose\lib\schema.js:1208:3)
at _.forEach (typegoosetest\node_modules\typegoose\lib\typegoose.js:40:25)
at arrayEach (typegoosetest\node_modules\typegoose\node_modules\lodash\lodash.js:537:11)
at Function.forEach (typegoosetest\node_modules\typegoose\node_modules\lodash\lodash.js:9359:14)
at User.getModelForClass (typegoosetest\node_modules\typegoose\lib\typegoose.js:39:19)
at Object. (typegoosetest\dist\server\app.js:32:26)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3

from typegoose.

szokodiakos avatar szokodiakos commented on July 20, 2024

Hello. Thanks for posting this. There was a missing initializer in the code which is now handled properly in f7706a3. I've tested this and it was working after the fix. Closing this one.

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.