Mongoose Calling function for default value
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
add a comment |
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
add a comment |
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
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
node.js mongoose
asked Jan 1 at 21:15
Sudhanshu GaurSudhanshu Gaur
2,63322246
2,63322246
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
But the thing is my IDE which is webstorm is blacking outdefaultline, 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 quotingdefault:"default" : fun
– robertklep
Jan 1 at 21:32
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
But the thing is my IDE which is webstorm is blacking outdefaultline, 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 quotingdefault:"default" : fun
– robertklep
Jan 1 at 21:32
add a comment |
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.
But the thing is my IDE which is webstorm is blacking outdefaultline, 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 quotingdefault:"default" : fun
– robertklep
Jan 1 at 21:32
add a comment |
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.
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.
answered Jan 1 at 21:20
robertkleprobertklep
138k18235245
138k18235245
But the thing is my IDE which is webstorm is blacking outdefaultline, 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 quotingdefault:"default" : fun
– robertklep
Jan 1 at 21:32
add a comment |
But the thing is my IDE which is webstorm is blacking outdefaultline, 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 quotingdefault:"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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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