Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Can't save records due to date cast error #502

Closed
thevinci opened this Issue · 7 comments

4 participants

@thevinci

My npm list shows that I have mongoose 2.1.0 installed. I am using the Date schema and it is going haywire. I think the solution for me is to use timestamps. Not sure what i am doing wrong here.

I have a schema as follows:

  Room = new Schema
    name: String
    description: String
    sabreId: String
    data:
      lastPrice: Number
      comm: String
      curr: String
      rateName: String
    created:
      type: Date
      default: Date.now
    valid:
      type: Boolean
      default: true

Then I do something like this:

    Room.find({}, function(err, r) {
      if( ! err) {
        console.log(r[0]);
        r[0].valid = false;
        r[0].save();
      }
    });

For the console.log, my dates come back as an object.
{
"name": "Black Room"
"description": "",
"created": {
"millisecond": 990,
"second": 49,
"minute": 35,
"hour": 8,
"day": 5,
"week": 36,
"month": 8,
"year": 2011
}
}

When I try to save again, I get a casting error.
{ stack: [Getter/Setter],
message: 'Cast to date failed for value "[object Object]"',
name: 'CastError',
type: 'date',
value:
{ millisecond: 636,
second: 25,
minute: 49,
hour: 8,
day: 5,
week: 36,
month: 8,
year: 2011 } }

@aheckmann
Owner

can't reproduce. heres my passing test: 95966c1

@aheckmann aheckmann closed this
@thevinci

When you grab records with dates using .find(), is it suppose to come back in an object format or there is something corrupted with my libraries? Like this:

{
"name": "TEST 2345",
"_id": "4e66dc1a2d54467469000149",
"valid": true,
"updated": {
"millisecond": 996,
"second": 28,
"minute": 51,
"hour": 2,
"day": 7,
"week": 36,
"month": 8,
"year": 2011
},
"created": {
"millisecond": 996,
"second": 28,
"minute": 51,
"hour": 2,
"day": 7,
"week": 36,
"month": 8,
"year": 2011
},
"free": false,
"tags": []
}

@aheckmann
Owner

something must be corrupt. whats in the db?

@thevinci

Ah. Sorry. I figured it out. Somehow my ajax code is sending datejs stuff over.

Thanks for the help! I love what you have done with Mongoose!

@aheckmann
Owner

anytime :D

@cleishm

For anyone looking at the issue in future - this exact issue will occur if you have datajs required anywhere in the application (or any module it uses).

One can argue this is the fault of datejs, as it changes the behaviour of the standard Date class. However, perhaps mongoose could work around it, given that an increasing number of modules depend on datejs (it's now available via npm).

@bithavoc

+1, date.js is causing the dates to be stored as plain objects. is there a workaround for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.