1

I'm not sure what's going wrong here / how I can fix it.

Parse.Cloud.job("expireTimes", function(request, response) {
var currentTime = new Date().getTime() / 1000;
var Wait = Parse.Object.extend("Wait");
Parse.Object.registerSubclass("Wait", Wait);
var query = new Parse.Query(Wait);
query.select(["times", "timestamp"]);
query.find({
    success: function(results) {
        for (var i = 0; i < timestamps.length; i++) {
            if (currentTime >= timestamps[i] + 60) {
                // Delete old wait times
                timestamps.splice(i, 1);
                times.splice(i, 1);
                i--;
            } else {
                break;
            }
        };
        response.success("Got " + results.length + " Query results: " + results[0].objectId);

    },
    error: function(error) {
        response.error("Request failed with response code");
        console.error("Request failed with response code ");
    }
});
});

It tell's me that timestamps is undefined:

I2015-11-27T17:00:03.419Z]Deployed v17 with triggers:
Jobs:
    expireTimes
E2015-11-27T17:01:04.489Z]v17 Ran job expireTimes with:
  Input: {}
  Result: ReferenceError: timestamps is not defined
    at e.query.find.success (main.js:9:24)
    at e.<anonymous> (Parse.js:14:28224)
    at e.s (Parse.js:14:27156)
    at e.n.value (Parse.js:14:26575)
    at e.s (Parse.js:14:27284)
    at e.n.value (Parse.js:14:26575)
    at e.s (Parse.js:14:27284)
    at e.n.value (Parse.js:14:26575)
    at e.<anonymous> (Parse.js:14:27228)
    at e.s (Parse.js:14:27156)

Not sure what this means / how to fix it. I would assume declare timestamps, but I thought that was already done with the retrieval of the arrays in the columns "times" and "timestamp"

Any help would be much appreciated. Thank you

bwc
  • 1,732
  • 1
  • 14
  • 27

1 Answers1

1

There's no such idea as query.select(). The only defined variables within the completion function are results (the array of retrieved parse objects) and currentTime, which you define in the enclosing scope.

The registerSubclass isn't useful unless you're actually defining a subclass.

As a starting exercise, try querying the class and logging the results. The query can be as simple as:

var query = new Parse.Query("Wait");
query.find().then(function(results) {
    console.log(JSON.stringify(results));
    var firstId = (results.length>0)? results[0].id : "n/a";  // notice "id", not "objectId"
    console.log("Got " + results.length + " First id: " + firstId);
    response.success(results);
}, function(error) {
    response.error(error);  // return the real error so we can look at it
});

If timestamp is a defined attribute on your Wait class, you can get it with get()...

// just like before where you got the id, try "getting" an attribute
var firstTimestamp = (results.length>0)? results[0].get("timestamp") : "n/a";
// remember, this depends on you defining a "timestamp" attribute on the class
danh
  • 62,181
  • 10
  • 95
  • 136
  • Both `timestamp` and `times` are arrays. `times` takes the user's time that they have submitted and `timestamp` just simply takes the time the user submitted it and puts it into an Int. That way, I can see if the `timestamp + 3600 < currentTime` and if it is, I delete it. So how do I grab those array's and run my for loop? – bwc Nov 27 '15 at 18:43
  • Not sure if this is helpful, but remember that all parse objects provide createdAt and updatedAt, and that queries can compare these with equality (but usually better) inequality, like `query.lessThan("updatedAt", someDate)`, where someDate is a JS date object. – danh Nov 27 '15 at 20:33
  • Also note that array properties can be gotten with get() and their contents looped like any JS array. – danh Nov 27 '15 at 20:34
  • Perfect. So now that I have changed my code [here](http://stackoverflow.com/questions/33964017/parse-update-object-from-cloud-code), how do I get it to actually delete and update the arrays in the object? – bwc Nov 27 '15 at 20:45
  • Got it. I'll answer there. One quick question, though, do you really want to do this to only one Wait object -- the first one found, `results[0]`? Or do you want to change all of the Wait objects found? – danh Nov 27 '15 at 20:48
  • Well the way I have it set up is that Wait is the overall class, and within that it has the properties of `name as String`, `times as Array`, `timestamp as Array`, `latitude as String`, and `longitude as String`. The application stores the times to a specific latitude and longitude and then averages them out. So I guess to answer your question, I want it to delete all instances within `Wait` that are older than an hour as they become inaccurate. – bwc Nov 27 '15 at 20:56