Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query fails due to "Tried to encode an invalid date." #3370

Closed
designunion opened this issue Jan 13, 2017 · 4 comments
Closed

Query fails due to "Tried to encode an invalid date." #3370

designunion opened this issue Jan 13, 2017 · 4 comments

Comments

@designunion
Copy link

designunion commented Jan 13, 2017

Query fails when an array column contains objects with dates. I have been able to isolate the data that generates the problem (see below). The problem does not occur with hosted-parse. The problem seems to be due to node modules based on the log information (see below).

The date formatting seems to be the issue:

  • Does not work: iso: ISODate("2014-11-15T07:35:03Z")
  • Works: iso: "2014-11-15T07:35:03Z"

The following is a closed issue which seems related but there was no resolution.
#1077

Steps to reproduce

  1. Run query find or get locally or via cloud code.

var Obj = Parse.Object.extend("Clock");
var query = new Parse.Query(Obj);
query.equalTo("objectId","2iVREi5Kiu");

query.find().then(function(result) {
res.success(result);
}, function(error) {
res.error(error);
});

OR

query.get("2iVREi5Kiu").then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
});

Expected Results

I expect the result field "history" to contain an array with an object that contains 2 dates. Instead the query fails.

[{ date: [should be a valid date],
event: 'Clock In - Sick Leave',
from: 'None',
to: [should be a valid date],
type: 'date',
who: 'Brian Login' }]

Actual Outcome

Console Log - Cloud
{code: 141, message: {}}

Console Log - Local
{className: "Error", _objCount: 8}

Console Log - Server
[{ date: Invalid Date,
event: 'Clock In - Sick Leave',
from: 'None',
to: Invalid Date,
type: 'date',
who: 'Brian Login' }]

CURL Results

curl -X GET
-H "X-Parse-Application-Id: xxx"
-H "X-Parse-Master-Key: xxx"
-G
--data-urlencode "where={"objectId":"2iVREi5Kiu"}"
https://xxx.nodechef.com/parse/classes/Clock

[{
"date": {
"__type": "Date",
"iso": {
"__type": "Date",
"iso": "2014-11-15T07:35:03.000Z"
}
},
"event": "Clock In - Sick Leave",
"from": "None",
"to": {
"__type": "Date",
"iso": {
"__type": "Date",
"iso": "2014-11-14T21:00:00.000Z"
}
},
"type": "date",
"who": "Brian Login"
}]

Stored Data - That Does Not Work

[{
date: {
__type: "Date",
iso: ISODate("2014-11-15T07:35:03Z")
},
event: "Clock In - Sick Leave",
from: "None",
to: {
iso: ISODate("2014-11-14T21:00:00Z"),
__type: "Date"
},
type: "date",
who: "Brian Login"
}]

Stored Data - That Will Work

[{
who: "Brian Login",
date: {
__type: "Date",
iso: "2014-11-15T02:35:03.026Z"
},
event: "Clock In - Sick Leave",
from: "None",
to: {
__type: "Date",
iso: "2014-11-14T16:00:00.000Z"
},
type: "date"
}]

Environment Setup

  • Server

    • parse-server version: 2.3.2
    • Node: Tried with 4.3.0, 4.3.2 and 6.9.4. The logs are from 6.9.4.
    • Parse JS SDK: 1.9.2
    • Operating System: OSX 10.11.6
    • Hardware: MacBook Pro
    • Localhost or remote server? Localhost
  • Database

    • MongoDB version: Unknown
    • Storage engine: RocksDb
    • Hardware: Unknown
    • Localhost or remote server? NodeChef

Logs/Trace

Error generating response. ParseError {
code: 141,
message:
Error: Tried to encode an invalid date.
at encode (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/encode.js:81:13)
at encode (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/encode.js:98:19)
at /Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/encode.js:91:14
at Array.map (native)
at encode (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/encode.js:90:18)
at exports.default (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/encode.js:16:10)
at Object.commitServerChanges (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/ObjectStateMutations.js:161:39)
at Object.commitServerChanges (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/UniqueInstanceStateController.js:162:24)
at ParseObjectSubclass.value (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/ParseObject.js:394:23)
at Function.value (/Users/brianmcgrath/parse-server-example/node_modules/parse-server/node_modules/parse/lib/node/ParseObject.js:1554:9) } code=141,
[object Object]

@designunion
Copy link
Author

I created a fix but I am not sure if it is the best solution. If you run Nodechef Parse Server version 2.2.25 this will not be a cloud code issue. Not sure what their fix is. However, you will have to make edits to your local server build and JS SDK.

  • Parse Server - edit decode.js module
  • Parse JS SDK Pre-compiled - edit version 1.9.2 (I do not use the npm module, assume you would also need to edit the decode.js module)

Old

if (value.__type === 'Date') {
return new Date(value.iso);
}

New Hack

if (value.__type === 'Date') {
if (value.iso.__type === 'Date') { // HACK // NESTED DATE OBJECT ISSUE
value.iso = value.iso.iso;
}
return new Date(value.iso);
}

Data Issue

Within an array I have objects that contain date objects. The problem is that the iso value is another date object, not an iso value. I am unclear if the data problem was something I did incorrectly or was a parse bug or ...

Bad Date
{
"date": {
"__type": "Date",
"iso": { // Iso value holds a date object
"__type": "Date",
"iso": "2014-11-15T07:35:03.000Z"
}

Good Date
{
"date": {
"__type": "Date",
"iso": "2014-11-15T07:35:03.000Z"
}

Data Fix

I am unclear what the best method is to repair the the "damaged data". I have 3 classes with this problem. I will most likely do a query in which I check if the offending fields have data (exists), select only those fields and re-save the returned arrays.

@flovilmart
Copy link
Contributor

@designunion thanks for reporting the issue!

@nodechefMatt
Copy link
Contributor

@designunion Matt here, I just submitted a pull request for this.
#3389

@stale
Copy link

stale bot commented Sep 18, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants