Skip to content

Problem with lessThan and greaterThan when using date fields #1456

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

Closed
3 tasks
rahilsharma opened this issue Apr 11, 2016 · 5 comments
Closed
3 tasks

Problem with lessThan and greaterThan when using date fields #1456

rahilsharma opened this issue Apr 11, 2016 · 5 comments

Comments

@rahilsharma
Copy link

Check out this issue for an ideal bug report. The closer your issue report is to that one, the more likely we are to be able to help, and the more likely we will be to fix the issue quickly!

For implementation related questions or technical support, please refer to the Stack Overflow and Server Fault communities.

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

Environment Setup

Running on local machine . I am using [email protected] , node @v4.4.2 , mongo 3.2.4.
I have my mongo db setup in ec2 .

Steps to reproduce

I ran the test on both parse.com cloud code and my local server.
This is the code that i was running

Parse.Cloud.define("lessthangreatthan", function (request, response){
    var userActivityQuery = new Parse.Query("Activity");
  var updatedAt=  request.params.updatedAt;
    var activityQuery = new Parse.Query("Activity");
    activityQuery.greaterThan("updatedAt", updatedAt);
    activityQuery.descending("updatedAt").limit(4);
    console.log(updatedAt);
    console.log(activityQuery);
    return activityQuery.find().then(function (activities) {
        response.success(activities);
    },function(err){
        response.error(err.message);
    });
});

When i run it on parse.com I get my results , but while running on local parse server it returns an empty

.

This is my console.log for activityQuery when i run it on parse.com
{"where":{"updatedAt":{"$gt":"2016-04-11T10:35:58.411Z"}},"limit":4,"order":"-updatedAt"}
This is my console.log for activityQuery when run on local parse server
ParseQuery { className: 'Activity',_where: { updatedAt: { '$gt': '2016-04-11T10:35:58.411Z' } },_include: [],_limit: 4,_skip: 0,_extraOptions: {}, _order: [ '-updatedAt' ] }

Logs/Trace

@hramos
Copy link
Contributor

hramos commented Apr 12, 2016

Is your Parse.com connected to the same database?

I think the best way to handle this issue going forward is to come up with a failing test case that shows that this query won't return the expected data. The sample issue linked at the top of the placeholder text in your original post has an example of this.

@rahilsharma
Copy link
Author

Is your Parse.com connected to the same database? - Yes , I migrated my database to ec2 mongo from parse , so it is using same database.

@rahilsharma
Copy link
Author

Environment Setup

Running on local machine . I am using [email protected] , node @v4.4.2 , mongo 3.2.4.
I have my mongo db setup in ec2 .

Steps to reproduce

So I made test app with a class Activity and the code that i am running.

Parse.Cloud.define("lessthangreatthan", function (request, response){

    var activityQuery = new Parse.Query("Activity");
    var updatedAt = request.params.updatedAt;
    activityQuery.greaterThan("updatedAt", updatedAt);
    console.log(updatedAt);
    console.log(activityQuery);
    return activityQuery.find().then(function (activities) {
        response.success(activities);
    },function(err){
        response.error(err.message);
    });
});

Parse.Cloud.define("output", function (request, response){
    var activityQuery = new Parse.Query("Activity");
    console.log(activityQuery);
    return activityQuery.find().then(function (activities) {
        response.success(activities);
    },function(err){
        response.error(err.message);
    });
});

Output of my Activity Class (used output function) when I called api.parse.com/1/output

{
  "result": [
    {
      "__type": "Object",
      "className": "Activity",
      "createdAt": "2016-04-12T12:15:58.065Z",
      "message": "h3",
      "objectId": "5P1sTXplqd",
      "updatedAt": "2016-04-12T12:32:20.430Z"
    },
    {
      "__type": "Object",
      "className": "Activity",
      "createdAt": "2016-04-12T12:15:48.860Z",
      "message": "h1",
      "objectId": "PyrtBMS7UW",
      "updatedAt": "2016-04-12T12:15:48.860Z"
    },
    {
      "__type": "Object",
      "className": "Activity",
      "createdAt": "2016-04-12T12:16:08.503Z",
      "message": "h4",
      "objectId": "hOJb49nXXO",
      "updatedAt": "2016-04-12T12:16:08.503Z"
    },
    {
      "__type": "Object",
      "className": "Activity",
      "createdAt": "2016-04-12T12:16:05.165Z",
      "message": "h2",
      "objectId": "lifargP8sn",
      "updatedAt": "2016-04-12T12:32:08.603Z"
    }
  ]
}

This is the output of my lessthangreatthan function when i called api.parse.com/1/lessthangreatthan
. My parameter was {"updatedAt":"2016-04-12T12:16:11.121Z"} so it returned rows which had updatedAt greater than the parameter value.

{
  "result": [
    {
      "__type": "Object",
      "className": "Activity",
      "createdAt": "2016-04-12T12:15:58.065Z",
      "message": "h3",
      "objectId": "5P1sTXplqd",
      "updatedAt": "2016-04-12T12:32:20.430Z"
    },
    {
      "__type": "Object",
      "className": "Activity",
      "createdAt": "2016-04-12T12:16:05.165Z",
      "message": "h2",
      "objectId": "lifargP8sn",
      "updatedAt": "2016-04-12T12:32:08.603Z"
    }
  ]
}

Now I ran the code on my local parse server (http://localhost:1338/parse/functions/output)
I am getting the same results.

{
  "result": [
    {
      "updatedAt": "2016-04-12T12:32:20.430Z", 
      "message": "h3",
      "createdAt": "2016-04-12T12:15:58.065Z",
      "objectId": "5P1sTXplqd",
      "__type": "Object",
      "className": "Activity"
    },
    {
      "createdAt": "2016-04-12T12:15:48.860Z",
      "updatedAt": "2016-04-12T12:15:48.860Z",
      "message": "h1",
      "objectId": "PyrtBMS7UW",
      "__type": "Object",
      "className": "Activity"
    },
    {
      "createdAt": "2016-04-12T12:16:08.503Z",
      "updatedAt": "2016-04-12T12:16:08.503Z",
      "message": "h4",
      "objectId": "hOJb49nXXO",
      "__type": "Object",
      "className": "Activity"
    },
    {
      "createdAt": "2016-04-12T12:16:05.165Z",
      "updatedAt": "2016-04-12T12:32:08.603Z",
      "message": "h2",
      "objectId": "lifargP8sn",
      "__type": "Object",
      "className": "Activity"
    }
  ]
}

But when i run lessthangreatthan on my local server (http://localhost:1338/parse/functions/lessthangreatthan) my response is empty

@hramos
Copy link
Contributor

hramos commented Apr 13, 2016

The request.params.updatedAt parameter you're passing to the function is a String, but the query constraint for dates expects a Date. Please update your Parse Server Cloud function to use the Date constructor to create a new Date object from the string in request.params.updatedAt.

The difference in behavior between hosted Parse Cloud Code could be explained by a difference in the version of the JavaScript SDK being used (see your Parse Cloud Code config to check which SDK you're loading in), or you may be running into the same issue raised in #655.

@hramos hramos closed this as completed Apr 13, 2016
@rahilsharma
Copy link
Author

Thanks, that fixed it.

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

2 participants