Mongoose Calling function for default value












0















I have a Mongoose Schema where I am assigning a default value to a field by calling a function.



My schema



function fun(){
let curr_time = moment().format();
return curr_time;
};

let flagged_trans_schema = new Schema({
time: {type: Date, default: fun},
});


I am facing a problem here, when I am writing default: fun() then the time field is not getting updated time, but when I am writing default: fun then time field value is updating every time.



Can anyone please tell me why it is happening?










share|improve this question



























    0















    I have a Mongoose Schema where I am assigning a default value to a field by calling a function.



    My schema



    function fun(){
    let curr_time = moment().format();
    return curr_time;
    };

    let flagged_trans_schema = new Schema({
    time: {type: Date, default: fun},
    });


    I am facing a problem here, when I am writing default: fun() then the time field is not getting updated time, but when I am writing default: fun then time field value is updating every time.



    Can anyone please tell me why it is happening?










    share|improve this question

























      0












      0








      0


      1






      I have a Mongoose Schema where I am assigning a default value to a field by calling a function.



      My schema



      function fun(){
      let curr_time = moment().format();
      return curr_time;
      };

      let flagged_trans_schema = new Schema({
      time: {type: Date, default: fun},
      });


      I am facing a problem here, when I am writing default: fun() then the time field is not getting updated time, but when I am writing default: fun then time field value is updating every time.



      Can anyone please tell me why it is happening?










      share|improve this question














      I have a Mongoose Schema where I am assigning a default value to a field by calling a function.



      My schema



      function fun(){
      let curr_time = moment().format();
      return curr_time;
      };

      let flagged_trans_schema = new Schema({
      time: {type: Date, default: fun},
      });


      I am facing a problem here, when I am writing default: fun() then the time field is not getting updated time, but when I am writing default: fun then time field value is updating every time.



      Can anyone please tell me why it is happening?







      node.js mongoose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 21:15









      Sudhanshu GaurSudhanshu Gaur

      2,63322246




      2,63322246
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Using default: fun() sets the default to the result of calling the fun function at the time the schema is declared. Just once, and the default will be that value for all newly created model instances (or, possibly, documents from the database that didn't have the time field set).



          Using default: fun sets the default to reference the fun function, and Mongoose is smart enough to know that this means that it should call that function each time a new model instance is created.






          share|improve this answer
























          • But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

            – Sudhanshu Gaur
            Jan 1 at 21:31











          • I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

            – robertklep
            Jan 1 at 21:32











          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%2f53998995%2fmongoose-calling-function-for-default-value%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









          1














          Using default: fun() sets the default to the result of calling the fun function at the time the schema is declared. Just once, and the default will be that value for all newly created model instances (or, possibly, documents from the database that didn't have the time field set).



          Using default: fun sets the default to reference the fun function, and Mongoose is smart enough to know that this means that it should call that function each time a new model instance is created.






          share|improve this answer
























          • But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

            – Sudhanshu Gaur
            Jan 1 at 21:31











          • I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

            – robertklep
            Jan 1 at 21:32
















          1














          Using default: fun() sets the default to the result of calling the fun function at the time the schema is declared. Just once, and the default will be that value for all newly created model instances (or, possibly, documents from the database that didn't have the time field set).



          Using default: fun sets the default to reference the fun function, and Mongoose is smart enough to know that this means that it should call that function each time a new model instance is created.






          share|improve this answer
























          • But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

            – Sudhanshu Gaur
            Jan 1 at 21:31











          • I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

            – robertklep
            Jan 1 at 21:32














          1












          1








          1







          Using default: fun() sets the default to the result of calling the fun function at the time the schema is declared. Just once, and the default will be that value for all newly created model instances (or, possibly, documents from the database that didn't have the time field set).



          Using default: fun sets the default to reference the fun function, and Mongoose is smart enough to know that this means that it should call that function each time a new model instance is created.






          share|improve this answer













          Using default: fun() sets the default to the result of calling the fun function at the time the schema is declared. Just once, and the default will be that value for all newly created model instances (or, possibly, documents from the database that didn't have the time field set).



          Using default: fun sets the default to reference the fun function, and Mongoose is smart enough to know that this means that it should call that function each time a new model instance is created.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 21:20









          robertkleprobertklep

          138k18235245




          138k18235245













          • But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

            – Sudhanshu Gaur
            Jan 1 at 21:31











          • I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

            – robertklep
            Jan 1 at 21:32



















          • But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

            – Sudhanshu Gaur
            Jan 1 at 21:31











          • I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

            – robertklep
            Jan 1 at 21:32

















          But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

          – Sudhanshu Gaur
          Jan 1 at 21:31





          But the thing is my IDE which is webstorm is blacking out default line, don't know why it is doing?

          – Sudhanshu Gaur
          Jan 1 at 21:31













          I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

          – robertklep
          Jan 1 at 21:32





          I have no idea either, I don't use WebStorm myself. Try quoting default: "default" : fun

          – robertklep
          Jan 1 at 21:32




















          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%2f53998995%2fmongoose-calling-function-for-default-value%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

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'