How to set up Typegoose









up vote
1
down vote

favorite












How do I setup Typegoose for a NodeJs REST API and typescript?



I always get weird error messages like MongoParseError: Incomplete key value pair for option or I do not receive any data, although there are some.



Can someone provide a complete example ?










share|improve this question

























    up vote
    1
    down vote

    favorite












    How do I setup Typegoose for a NodeJs REST API and typescript?



    I always get weird error messages like MongoParseError: Incomplete key value pair for option or I do not receive any data, although there are some.



    Can someone provide a complete example ?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      How do I setup Typegoose for a NodeJs REST API and typescript?



      I always get weird error messages like MongoParseError: Incomplete key value pair for option or I do not receive any data, although there are some.



      Can someone provide a complete example ?










      share|improve this question













      How do I setup Typegoose for a NodeJs REST API and typescript?



      I always get weird error messages like MongoParseError: Incomplete key value pair for option or I do not receive any data, although there are some.



      Can someone provide a complete example ?







      node.js mongodb typescript mongoose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 1:34









      Frederic Reisenhauer

      155




      155






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          If you want to programm an API under the following conditions, you can use the provided minimal example below:



          Conditions:

          NodeJS
          Typescript
          MongoDB (locally)



          Example:



          If you are working with typescript, a project structure, similar to this is recommended:



          ├── dist
          | ├── (your compiled JS)
          ├── src
          | ├── models
          | | ├── user.model.ts
          | ├── user-repository.ts
          | ├── app.ts
          | ├── index.ts
          ├── test
          | ├── (your tests)


          Furthermore, it's required to install the following packages



          npm install --save typescript mongoose express @types/express @types/mongoose typegoose



          index.ts:



          This file, I just have for bootstraping purposes



          import app from './app';

          const port = 3000;

          app.listen(port, (err) =>
          if (err)
          return console.log(err);

          return console.log('Server up and running on ' + port);
          );


          app.ts:



          Here, the actual logic is going on



          import UserRepository from './user-repository';
          import * as express from 'express';

          export class App
          public app;

          constructor()
          this.app = express();
          this.config();
          this.mountRoutes();


          private mountRoutes()
          // Definition of the possible API routes
          this.app.route('/users')
          .get((req, res) =>
          var repo = new UserRepository();
          // we catch the result with the typical "then"
          repo.getUsers().then((x) =>
          // .json(x) instead of .send(x) should also be okay
          res.status(200).send(x);
          );
          );

          // here with parameter
          //


          export default new App().app;


          user-repository.ts



          import * as mongoose from 'mongoose';
          import User, UserModel from './models/user.model';


          export class UserRepository
          constructor()

          async getUser(id: number): Promise<User>
          // v
          return UserModel.findOne("userId": id);


          async getUsers(): Promise<User>
          return UserModel.find();




          user.model.ts



          import * as mongoose from 'mongoose';
          import prop, Typegoose from 'typegoose';


          export class User extends Typegoose
          // properties
          //

          export const UserModel = new User().getModelForClass(User,
          // v
          schemaOptions: collection: 'users'
          )


          As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245096%2fhow-to-set-up-typegoose%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            If you want to programm an API under the following conditions, you can use the provided minimal example below:



            Conditions:

            NodeJS
            Typescript
            MongoDB (locally)



            Example:



            If you are working with typescript, a project structure, similar to this is recommended:



            ├── dist
            | ├── (your compiled JS)
            ├── src
            | ├── models
            | | ├── user.model.ts
            | ├── user-repository.ts
            | ├── app.ts
            | ├── index.ts
            ├── test
            | ├── (your tests)


            Furthermore, it's required to install the following packages



            npm install --save typescript mongoose express @types/express @types/mongoose typegoose



            index.ts:



            This file, I just have for bootstraping purposes



            import app from './app';

            const port = 3000;

            app.listen(port, (err) =>
            if (err)
            return console.log(err);

            return console.log('Server up and running on ' + port);
            );


            app.ts:



            Here, the actual logic is going on



            import UserRepository from './user-repository';
            import * as express from 'express';

            export class App
            public app;

            constructor()
            this.app = express();
            this.config();
            this.mountRoutes();


            private mountRoutes()
            // Definition of the possible API routes
            this.app.route('/users')
            .get((req, res) =>
            var repo = new UserRepository();
            // we catch the result with the typical "then"
            repo.getUsers().then((x) =>
            // .json(x) instead of .send(x) should also be okay
            res.status(200).send(x);
            );
            );

            // here with parameter
            //


            export default new App().app;


            user-repository.ts



            import * as mongoose from 'mongoose';
            import User, UserModel from './models/user.model';


            export class UserRepository
            constructor()

            async getUser(id: number): Promise<User>
            // v
            return UserModel.findOne("userId": id);


            async getUsers(): Promise<User>
            return UserModel.find();




            user.model.ts



            import * as mongoose from 'mongoose';
            import prop, Typegoose from 'typegoose';


            export class User extends Typegoose
            // properties
            //

            export const UserModel = new User().getModelForClass(User,
            // v
            schemaOptions: collection: 'users'
            )


            As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.






            share|improve this answer
























              up vote
              0
              down vote













              If you want to programm an API under the following conditions, you can use the provided minimal example below:



              Conditions:

              NodeJS
              Typescript
              MongoDB (locally)



              Example:



              If you are working with typescript, a project structure, similar to this is recommended:



              ├── dist
              | ├── (your compiled JS)
              ├── src
              | ├── models
              | | ├── user.model.ts
              | ├── user-repository.ts
              | ├── app.ts
              | ├── index.ts
              ├── test
              | ├── (your tests)


              Furthermore, it's required to install the following packages



              npm install --save typescript mongoose express @types/express @types/mongoose typegoose



              index.ts:



              This file, I just have for bootstraping purposes



              import app from './app';

              const port = 3000;

              app.listen(port, (err) =>
              if (err)
              return console.log(err);

              return console.log('Server up and running on ' + port);
              );


              app.ts:



              Here, the actual logic is going on



              import UserRepository from './user-repository';
              import * as express from 'express';

              export class App
              public app;

              constructor()
              this.app = express();
              this.config();
              this.mountRoutes();


              private mountRoutes()
              // Definition of the possible API routes
              this.app.route('/users')
              .get((req, res) =>
              var repo = new UserRepository();
              // we catch the result with the typical "then"
              repo.getUsers().then((x) =>
              // .json(x) instead of .send(x) should also be okay
              res.status(200).send(x);
              );
              );

              // here with parameter
              //


              export default new App().app;


              user-repository.ts



              import * as mongoose from 'mongoose';
              import User, UserModel from './models/user.model';


              export class UserRepository
              constructor()

              async getUser(id: number): Promise<User>
              // v
              return UserModel.findOne("userId": id);


              async getUsers(): Promise<User>
              return UserModel.find();




              user.model.ts



              import * as mongoose from 'mongoose';
              import prop, Typegoose from 'typegoose';


              export class User extends Typegoose
              // properties
              //

              export const UserModel = new User().getModelForClass(User,
              // v
              schemaOptions: collection: 'users'
              )


              As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                If you want to programm an API under the following conditions, you can use the provided minimal example below:



                Conditions:

                NodeJS
                Typescript
                MongoDB (locally)



                Example:



                If you are working with typescript, a project structure, similar to this is recommended:



                ├── dist
                | ├── (your compiled JS)
                ├── src
                | ├── models
                | | ├── user.model.ts
                | ├── user-repository.ts
                | ├── app.ts
                | ├── index.ts
                ├── test
                | ├── (your tests)


                Furthermore, it's required to install the following packages



                npm install --save typescript mongoose express @types/express @types/mongoose typegoose



                index.ts:



                This file, I just have for bootstraping purposes



                import app from './app';

                const port = 3000;

                app.listen(port, (err) =>
                if (err)
                return console.log(err);

                return console.log('Server up and running on ' + port);
                );


                app.ts:



                Here, the actual logic is going on



                import UserRepository from './user-repository';
                import * as express from 'express';

                export class App
                public app;

                constructor()
                this.app = express();
                this.config();
                this.mountRoutes();


                private mountRoutes()
                // Definition of the possible API routes
                this.app.route('/users')
                .get((req, res) =>
                var repo = new UserRepository();
                // we catch the result with the typical "then"
                repo.getUsers().then((x) =>
                // .json(x) instead of .send(x) should also be okay
                res.status(200).send(x);
                );
                );

                // here with parameter
                //


                export default new App().app;


                user-repository.ts



                import * as mongoose from 'mongoose';
                import User, UserModel from './models/user.model';


                export class UserRepository
                constructor()

                async getUser(id: number): Promise<User>
                // v
                return UserModel.findOne("userId": id);


                async getUsers(): Promise<User>
                return UserModel.find();




                user.model.ts



                import * as mongoose from 'mongoose';
                import prop, Typegoose from 'typegoose';


                export class User extends Typegoose
                // properties
                //

                export const UserModel = new User().getModelForClass(User,
                // v
                schemaOptions: collection: 'users'
                )


                As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.






                share|improve this answer












                If you want to programm an API under the following conditions, you can use the provided minimal example below:



                Conditions:

                NodeJS
                Typescript
                MongoDB (locally)



                Example:



                If you are working with typescript, a project structure, similar to this is recommended:



                ├── dist
                | ├── (your compiled JS)
                ├── src
                | ├── models
                | | ├── user.model.ts
                | ├── user-repository.ts
                | ├── app.ts
                | ├── index.ts
                ├── test
                | ├── (your tests)


                Furthermore, it's required to install the following packages



                npm install --save typescript mongoose express @types/express @types/mongoose typegoose



                index.ts:



                This file, I just have for bootstraping purposes



                import app from './app';

                const port = 3000;

                app.listen(port, (err) =>
                if (err)
                return console.log(err);

                return console.log('Server up and running on ' + port);
                );


                app.ts:



                Here, the actual logic is going on



                import UserRepository from './user-repository';
                import * as express from 'express';

                export class App
                public app;

                constructor()
                this.app = express();
                this.config();
                this.mountRoutes();


                private mountRoutes()
                // Definition of the possible API routes
                this.app.route('/users')
                .get((req, res) =>
                var repo = new UserRepository();
                // we catch the result with the typical "then"
                repo.getUsers().then((x) =>
                // .json(x) instead of .send(x) should also be okay
                res.status(200).send(x);
                );
                );

                // here with parameter
                //


                export default new App().app;


                user-repository.ts



                import * as mongoose from 'mongoose';
                import User, UserModel from './models/user.model';


                export class UserRepository
                constructor()

                async getUser(id: number): Promise<User>
                // v
                return UserModel.findOne("userId": id);


                async getUsers(): Promise<User>
                return UserModel.find();




                user.model.ts



                import * as mongoose from 'mongoose';
                import prop, Typegoose from 'typegoose';


                export class User extends Typegoose
                // properties
                //

                export const UserModel = new User().getModelForClass(User,
                // v
                schemaOptions: collection: 'users'
                )


                As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 1:37









                Frederic Reisenhauer

                155




                155



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245096%2fhow-to-set-up-typegoose%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党