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

Closed
thevinci opened this Issue Sep 5, 2011 · 7 comments

Comments

Projects
None yet
4 participants

thevinci commented Sep 5, 2011

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 aheckmann added a commit that referenced this issue Sep 7, 2011

@aheckmann aheckmann add passing test for #502 95966c1
Collaborator

aheckmann commented Sep 7, 2011

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

aheckmann closed this Sep 7, 2011

thevinci commented Sep 8, 2011

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": []
}

Collaborator

aheckmann commented Sep 8, 2011

something must be corrupt. whats in the db?

thevinci commented Sep 8, 2011

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!

Collaborator

aheckmann commented Sep 9, 2011

anytime :D

Contributor

cleishm commented Dec 12, 2011

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).

+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