Where do i define function for mongoose schema










0















I'm just asking myself a question of what is the correct way of defining a function for a mongoose schema.



Lets take my UserSchema for example. In many of my routes i'd like to get information of the user so that i do a query getUserByUsername which include a findOne(username: username).



As i wrote, i am doing this in many routes. So to shorten my code i'd like to have this function just one time and not inside every route again and again. I want a central place from which i can call this function whenever i want.



So i started searching and found out, that it's valid to add functions directly inside my user.js which is my UserSchema definition.



The whole file looks like this:



user.js



const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const config = require('../config/database');

const Partner = require('./partner');
const UserRights = require('./userRights');

//User Schema - Datenbankaufbau
const UserSchema = mongoose.Schema(
name:
type: String
,
email:
type: String,
required: true
,
username:
type: String,
required: true
,
password:
type: String,
required: true
,
partnerId:
type: mongoose.Schema.Types.ObjectId,
ref: 'Partner'
,
userRights:
type: mongoose.Schema.Types.ObjectId,
ref: 'UserRights'
,
isLoggedIn:
type: Boolean,
default: false
,
hasToRelog:
type: Boolean,
default: false

);



const User = module.exports = mongoose.model('User', UserSchema);


// Find User by ID
module.exports.getUserById = function(id, callback)
User.findById(id, callback);


// Find User by Username
module.exports.getUserByUsername = function(username, callback)
const query = username: username;
User.findOne(query, callback);



But i now want to know, if this is a correct way of storing functions or if there is a better / other way?










share|improve this question


























    0















    I'm just asking myself a question of what is the correct way of defining a function for a mongoose schema.



    Lets take my UserSchema for example. In many of my routes i'd like to get information of the user so that i do a query getUserByUsername which include a findOne(username: username).



    As i wrote, i am doing this in many routes. So to shorten my code i'd like to have this function just one time and not inside every route again and again. I want a central place from which i can call this function whenever i want.



    So i started searching and found out, that it's valid to add functions directly inside my user.js which is my UserSchema definition.



    The whole file looks like this:



    user.js



    const mongoose = require('mongoose');
    const bcrypt = require('bcryptjs');
    const config = require('../config/database');

    const Partner = require('./partner');
    const UserRights = require('./userRights');

    //User Schema - Datenbankaufbau
    const UserSchema = mongoose.Schema(
    name:
    type: String
    ,
    email:
    type: String,
    required: true
    ,
    username:
    type: String,
    required: true
    ,
    password:
    type: String,
    required: true
    ,
    partnerId:
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Partner'
    ,
    userRights:
    type: mongoose.Schema.Types.ObjectId,
    ref: 'UserRights'
    ,
    isLoggedIn:
    type: Boolean,
    default: false
    ,
    hasToRelog:
    type: Boolean,
    default: false

    );



    const User = module.exports = mongoose.model('User', UserSchema);


    // Find User by ID
    module.exports.getUserById = function(id, callback)
    User.findById(id, callback);


    // Find User by Username
    module.exports.getUserByUsername = function(username, callback)
    const query = username: username;
    User.findOne(query, callback);



    But i now want to know, if this is a correct way of storing functions or if there is a better / other way?










    share|improve this question
























      0












      0








      0








      I'm just asking myself a question of what is the correct way of defining a function for a mongoose schema.



      Lets take my UserSchema for example. In many of my routes i'd like to get information of the user so that i do a query getUserByUsername which include a findOne(username: username).



      As i wrote, i am doing this in many routes. So to shorten my code i'd like to have this function just one time and not inside every route again and again. I want a central place from which i can call this function whenever i want.



      So i started searching and found out, that it's valid to add functions directly inside my user.js which is my UserSchema definition.



      The whole file looks like this:



      user.js



      const mongoose = require('mongoose');
      const bcrypt = require('bcryptjs');
      const config = require('../config/database');

      const Partner = require('./partner');
      const UserRights = require('./userRights');

      //User Schema - Datenbankaufbau
      const UserSchema = mongoose.Schema(
      name:
      type: String
      ,
      email:
      type: String,
      required: true
      ,
      username:
      type: String,
      required: true
      ,
      password:
      type: String,
      required: true
      ,
      partnerId:
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Partner'
      ,
      userRights:
      type: mongoose.Schema.Types.ObjectId,
      ref: 'UserRights'
      ,
      isLoggedIn:
      type: Boolean,
      default: false
      ,
      hasToRelog:
      type: Boolean,
      default: false

      );



      const User = module.exports = mongoose.model('User', UserSchema);


      // Find User by ID
      module.exports.getUserById = function(id, callback)
      User.findById(id, callback);


      // Find User by Username
      module.exports.getUserByUsername = function(username, callback)
      const query = username: username;
      User.findOne(query, callback);



      But i now want to know, if this is a correct way of storing functions or if there is a better / other way?










      share|improve this question














      I'm just asking myself a question of what is the correct way of defining a function for a mongoose schema.



      Lets take my UserSchema for example. In many of my routes i'd like to get information of the user so that i do a query getUserByUsername which include a findOne(username: username).



      As i wrote, i am doing this in many routes. So to shorten my code i'd like to have this function just one time and not inside every route again and again. I want a central place from which i can call this function whenever i want.



      So i started searching and found out, that it's valid to add functions directly inside my user.js which is my UserSchema definition.



      The whole file looks like this:



      user.js



      const mongoose = require('mongoose');
      const bcrypt = require('bcryptjs');
      const config = require('../config/database');

      const Partner = require('./partner');
      const UserRights = require('./userRights');

      //User Schema - Datenbankaufbau
      const UserSchema = mongoose.Schema(
      name:
      type: String
      ,
      email:
      type: String,
      required: true
      ,
      username:
      type: String,
      required: true
      ,
      password:
      type: String,
      required: true
      ,
      partnerId:
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Partner'
      ,
      userRights:
      type: mongoose.Schema.Types.ObjectId,
      ref: 'UserRights'
      ,
      isLoggedIn:
      type: Boolean,
      default: false
      ,
      hasToRelog:
      type: Boolean,
      default: false

      );



      const User = module.exports = mongoose.model('User', UserSchema);


      // Find User by ID
      module.exports.getUserById = function(id, callback)
      User.findById(id, callback);


      // Find User by Username
      module.exports.getUserByUsername = function(username, callback)
      const query = username: username;
      User.findOne(query, callback);



      But i now want to know, if this is a correct way of storing functions or if there is a better / other way?







      mongoose mongoose-schema






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 11:20









      SithysSithys

      2,39431841




      2,39431841






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You should make a controller folder in which you will define the functions,and those functions will be called in route class in making any request.You can have a detailed idea in the below article.



          https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes





          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',
            autoActivateHeartbeat: false,
            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%2f53279904%2fwhere-do-i-define-function-for-mongoose-schema%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









            0














            You should make a controller folder in which you will define the functions,and those functions will be called in route class in making any request.You can have a detailed idea in the below article.



            https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes





            share|improve this answer



























              0














              You should make a controller folder in which you will define the functions,and those functions will be called in route class in making any request.You can have a detailed idea in the below article.



              https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes





              share|improve this answer

























                0












                0








                0







                You should make a controller folder in which you will define the functions,and those functions will be called in route class in making any request.You can have a detailed idea in the below article.



                https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes





                share|improve this answer













                You should make a controller folder in which you will define the functions,and those functions will be called in route class in making any request.You can have a detailed idea in the below article.



                https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 11:28









                Saad MaqboolSaad Maqbool

                1869




                1869



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279904%2fwhere-do-i-define-function-for-mongoose-schema%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto