javascript - Mongoose Default Date - Stack Overflow

Is there a specific setting you need to setup in order for mongoose to have this below Model working?

Is there a specific setting you need to setup in order for mongoose to have this below Model working?

// The Model

let theModelSchema = new Schema({
  created_on: {
    // type: Date, // Either Date or Number
    type: Number,
    default: Date.now(), // have also tried Date.now
  },
})

let TheModel = mongoose.model('TheModel', theModelSchema)

// Save first entry

let newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date will be for example 123 here
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // 123
})

// Save second entry a little bit later

newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date should be 125(or whatever)
// But for some reason it stays as 123 and only updates // // when the server restats
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // still 123, should be different
})

Is there a specific setting you need to setup in order for mongoose to have this below Model working?

// The Model

let theModelSchema = new Schema({
  created_on: {
    // type: Date, // Either Date or Number
    type: Number,
    default: Date.now(), // have also tried Date.now
  },
})

let TheModel = mongoose.model('TheModel', theModelSchema)

// Save first entry

let newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date will be for example 123 here
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // 123
})

// Save second entry a little bit later

newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date should be 125(or whatever)
// But for some reason it stays as 123 and only updates // // when the server restats
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // still 123, should be different
})

It works for me when I run it the first time, but it then saves another entry, it doesn't update the time in the new entry, it keeps the old time, as long as the same server instance is running.

I have tried: delete newEntry after the code runs

So it seems that the server chaches the new Date and uses it on every entry The mongoose documentation tells me I am doing this right, as well as many other posts. http://mongoosejs./docs/defaults.html

So what am I doing wrong here? I of course know of many workarounds, but you want your model to have this automated. Help is greatly appreciated, thanks in advance!

Share Improve this question asked Jan 26, 2018 at 12:16 Mark OwuyaMark Owuya 712 silver badges5 bronze badges 1
  • it will not update the same doc, insted it will create new doc with the new Date. – Saikat Chakrabortty Commented Jan 26, 2018 at 12:22
Add a ment  | 

1 Answer 1

Reset to default 5

Ok, kind of lame to respond to your own question...this is weird but it started working now.

I updated mongoose (I don't know if this had anything to do with it)

I also removed the: delete newEntry So that seemed to just be redundant

However, this seem to have worked this time around

created_on: {
  type: Number,
  // Changing Date.now() to Date.now did the trick this time around
  default: Date.now, 
},

So I only changed Date.now() to Date.now, which I by the way had already tried before...so maybe the mongoose update made a difference? If anyone knows why this happened, please let me know.

Thanks

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745266833a4619507.html

相关推荐

  • javascript - Mongoose Default Date - Stack Overflow

    Is there a specific setting you need to setup in order for mongoose to have this below Model working?

    12小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信