0

I'm novice in programming. Right now I'm developing my first app with Swift and Parse (for backend). My app is for the survey to know the confidence rating of the politician. The rating is equal to those who voted YES percentage within last day. I want to do all the math on the cloud and just send client the result (a number). So that I've researched the docs on Parse but not quite sure what to do. Below is my tryout (not successful though) and I hope you could help me to find and fix the problems.

So I have the score class (table). A single score object look like:

{
  "objectID": 12eroi87,
  "vote": 1, // or 0
  "createdAt": Nov 5, 2014, 10:35 // Date
}

Now I have to build some cloud function to do all the math.

Parse.Cloud.define("confidenceRating", function(request, response) {
    var query = new Parse.Query("score");
    query.equalTo("vote", request.params.vote);
    query.find({
        success: function(results) {
            var sum = 0;
            for (var i = 0; i < results.length; ++i) {
                sum += results[i].get("vote");
            }
            response.success(100 * sum / results.length);
        },
        error: function() {
            response.error("something went wrong");
        }
    });
});

I'm aware the code above is some mess (( No idea how to cut the votes within last day. And finally I have no idea how to call the function with Swift.

Hope I could help! I'd appreciate much if you provide some useful links as well.

2 Answers2

0

Look here for an example of calling a cloud code function in Swift: Calling Parse cloud functions very slow on iOS in mobile network

Community
  • 1
  • 1
0

in order to get records for the specific date you will need to filter by date, below might work with some adaptation to your needs:

var transformedDate = moment(request.params.selectedDate);
var transformedDate2 = moment(request.params.selectedDate).add(1, 'days');
//console.log("transformedDate: " + transformedDate.format());
//console.log("transformedDate2: " + transformedDate2.format());
//console.log("selectedDate: " + selectedDate);
var query = new Parse.Query("score");
query.greaterThan("createdAt", new Date(transformedDate.format("YYYY-MM-DDT00:00:00.000Z")));
query.lessThan("createdAt", new Date(transformedDate2.format("YYYY-MM-DDT00:00:00.000Z")));

I did solved something similar with dates in filter-on-own-date-fields-no-working-and-or-wrong-date-format-used

your friend here will be moment.js module, carefull to use the latest momentjs version to avoid problems as those in trouble-using-the-moment-module

Community
  • 1
  • 1
Chasky
  • 60
  • 6