How to get the column of parse.com class? - javascript

I trying to use Cloud Code to get class's data.
And there are my questions.
Q:In this photo,how do I get the score column?
there is my code.
var object = [];
var pushQuery = new Parse.Query('Meeting');
pushQuery.find({
success: function(results) {
success: function(results) {
for (var i = 0; i < results.length; i++) {
object[i] = results[i];
}
},
error: function(error) {
}
});

You can get column from parse one of following ways.
results[i].get("columnName").
or
results[i].ansId //replace ansId with your column name.

Related

Parse.com JS looping through an array when using query.find()

I'm trying to query using each elements on the array as the constraint, but so far its has only given me one result.
Here is the array:
["C44","C43","C45","C117"]
Here what I have:
{var TestObject = Parse.Object.extend("TestObject");
var query = new Parse.Query(TestObject);
query.get("W7yuUsbbav", {
success: function(testObject) {
var subjectArray = testObject.get("subjects");
for (i = 0; i < subjectArray.length ; i++ ){
console.log(subjectArray[i]);
var query = new Parse.Query("Subjects");
query.equalTo("subCode", subjectArray[i]);
query.find({
success: function(results) {
$scope.$apply(function() {
$scope.subjects1 = results.map(function(obj) {
return {subCode: obj.get("subCode"), subName: obj.get("subName"), subDesc: obj.get("subDesc")};
});
});
},
error: function(error) {
}
});
}
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});}
You can use the containedIn property of query.
var sampleId = "W7yuUsbbav";
var query1 = new Parse.Query("TestObject");
query1.get(sampleId).then(function (testObject) {
// Success
var subjectArray = testObject.get("subjects");
var query2 = new Parse.Query("Subjects");
query2.containedIn("subCode", subjectArray);
return query2.find();
}).then(function (subjectObjects) {
// Success
// results is an array of objects
}, function (error) {
// Error
});

Another Parse 'success/error not called' error

I recently posted an issue I had with another Parse CloudCode method, were the error was thrown that Error: success/error was not called. I am having that issue again but with a different method/scenario.
Parse.Cloud.define("background", function(request, response) {
var moments = require("cloud/moments.js");
var now = moments.moment();
var query = new Parse.Query("Group");
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var events = object.get("Events");
var getUsers = false;
for (var q = 0; q < events.length; q++) {
var e = events[q];
if (e.get("date") == now) {
getUsers = true;
break;
}
}
if (getUsers == true) {
for (var q = 0; q < events.length; q++) {
var e = events[q];
if (e.get("date") == now) {
var relation = object.relation("created");
var partOne = e.get("name");
var outString1 = partOne.concat(" is now");
// generate a query based on that relation
var query = relation.query();
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: outString1
}
}, {
success: function() {
// Push was successful
},
error: function(error) {
// Handle error
}
});
var relation2 = object.relation("joined");
var partOnee = e.get("name");
var outString = partOnee.concat(" is now");
// generate a query based on that relation
var query2 = relation.query();
Parse.Push.send({
where: query2, // Set our Installation query
data: {
alert: outString
}
}, {
success: function() {
// Push was successful
},
error: function(error) {
// Handle error
}
});
e.destroy();
}
}
}
}
}
});
response.success();
});
Since this method involves more than just a simple query and return (as it has the for loop among other things) I am a bit confused on how to implement the Parse Promise stuff. If anyone could assist me in how I should go about implementing the promise stuff it would be much appreciated.
Parse documentation is very clear on how to use Promises and how to rewrite your pyramid code with .then() blocks instead.

many-to-many relation on Parse.com Cloud Code

I have a class "Team", which can have many Users.
I need to create a from/to relation every time a user is added to a Team. Every user can rate and be rated by all his teammates.
This is my Cloud code. I can't seem to get this working.
// add Team relations for every saved player
Parse.Cloud.afterSave("Team", function(request) {
// Team being saved
var team = request.object,
relation = team.relation("players"),
query = relation.query();
query.find({
// players on this Team
success: function(results) {
// all players on this Team
var i, j, player, teammate;
for (i = 0; i < results.length; i++) {
player = results[i];
// for each player add a new Rate relation (from/to)
for (j = 0; j < results.length; j++) {
teammate = results[i];
// if it's same player, escape relation
if (player == teammate) { continue; }
var Rate = Parse.Object.extend("Rate"),
rate = new Rate();
// create the relation from this player
var fromRelation = rate.relation('from');
fromRelation.add(player);
// to every other player on the team
var toRelation = rate.relation('to');
toRelation.add(teammate);
rate.save(null, {
success: function(_team) {
},
error: function(_team, error) {
}
});
}
}
response.success('rate relation success');
},
error: function(error) {
response.error('rate relation error');
}
});
});
Adding a player to a team is working great, it runs on Client side.
Classes setup on Data Browser:
Your class is "Rates" but you are using this code:
var Rate = Parse.Object.extend("Rate"),
rate = new Rate();
change to
var Rate = Parse.Object.extend("Rates"),
rate = new Rate();
Also, make sure your object is created before adding a relation.
var Rate = Parse.Object.extend("Rate"),
rate = new Rate();
[.... MOVED BELOW .....}
rate.save(null, {
success: function(_team) {
[.... now create and save relations ...]
},
Solved it this way.
The only thing annoying me is the fact that if I try this, I get an error:
"fromRelation is expecting _User, but got _Team", why?
player = results[i];
var fromRelation = rate.relation('from');
fromRelation.add(player);
I had to create 2 new vars, fromPlayer and toPlayer, although it seems dumb to me, anyway it works.
Parse.Cloud.define("updateTeamPlayers", function(request, response) {
var Team = Parse.Object.extend("Team"),
team = new Team();
team.id = request.params.id;
var relation = team.relation("players"),
query = relation.query();
query.find({
success: function(results) {
var i, j, player, teammate, queryArray = [], teammateRes = results;
for (i = 0; i < results.length; i++) {
player = results[i];
// for each player add a new Rate relation (from/to)
for (j = 0; j < teammateRes.length; j++) {
teammate = teammateRes[j];
// if it's same player, escape relation
if (player == teammate) { continue; }
var Rate = Parse.Object.extend("Rates"),
rate = new Rate();
// create the relation from this player
var fromPlayer = new Parse.User();
fromPlayer.id = player.id;
var fromRelation = rate.relation('from');
fromRelation.add(fromPlayer);
// to every other player on the team
var toPlayer = new Parse.User();
toPlayer.id = teammate.id;
var toRelation = rate.relation('to');
toRelation.add(toPlayer);
queryArray.push(rate);
}
}
Parse.Object.saveAll(queryArray, {
success: function(_allsaved) {
response.success('rate save success:' + _allsaved);
},
error: function(error) {
response.error('rate save error:' + error + ' =|= ' + error.message);
}
});
// end suucess
},
error: function() {
response.error("team failed");
}
});
});

using variable to change Parse.com query

I am using the following Parse.com Javascript query and need to switch the type of query based on a variable.
function searchParseExercises (queryParam, ActiveFilters, searchParam) {
var exercise = [];
var TagSearch = Parse.Object.extend("Exercises");
var query = new Parse.Query(TagSearch);
query.containsAll("tags", ActiveFilters);
query.limit(20);
query.find({
success: function(results) {
var exerciseData = {};
for (var i = 0; i < results.length; i++) {
var object = results[i];
var exerciseData = {
exerciseName : object.get('exerciseName'),
exerciseDescription : object.get('exerciseDescription'),
images : object.get('images'),
}
exercise.push(exerciseData);
}
$scope.allExercises = exercise;
},
error: function(error) {
$ionicPopup.alert({
title: "Error: " + error.code + " " + error.message
});
}
});
}
To clarify the requirement I have both a text search and filter search in my template. If there is a text search then it should perform:
query.contains("exerciseName", searchParam);
If there are ActiveFilters then it should perform this:
query.containsAll("tags", ActiveFilters);
However if both variables are present (searchParam and ActiveFilters)
then it should perform a Compound Query.
I have no idea how I can wire all this up cleanly.
What I understood from your question:
if (ActiveFilters && searchParam) {
Parse.Query.or(ActiveFilters, searchParam).find({
success: function(results) {
// results contains a list of players that either have won a lot of games or won only a few games.
},
error: function(error) {
// There was an error.
}
}
else if (ActiveFilters) {
query.containsAll("tags", ActiveFilters);
}
else if (searchParam) {
query.contains("exerciseName", searchParam);
}

How can I update data in a for loop when my data is returned with a defer after the loop completes

I have two arrays:
$scope.grid.data and $scope.grid.backup
I use the following script to compare the data in each one element at a time:
for (var i = 0, len = $scope.grid.data.length; i < len; i++) {
if (!angular.equals($scope.grid.data[i], $scope.grid.backup[i])) {
var rowData = $scope.grid.data[i]
var idColumn = $scope.entityType.toLowerCase() + 'Id';
var entityId = rowData[idColumn];
entityService.putEntity($scope.entityType, entityId, $scope.grid.data[i])
.then(function (result) {
angular.copy(result, $scope.grid.data[i]);
angular.copy(result, $scope.grid.backup[i]);
}, function (result) {
alert("Error: " + result);
})
}
}
and the following to update the database:
putEntity: function (entityType, entityId, entity) {
var deferred = $q.defer();
EntityResource.putEntity({ entityType: entityType, entityId: entityId }, entity,
function (resp) {
deferred.resolve(resp);
}, function (resp) {
deferred.reject('Error updating');
});
return deferred.promise;
}
This script correctly notices the changes and updates the database.
However there is a problem when the putEntity returns with a result and it then tries to copy the result into $scope.grid.data[i] and
$scope.grid.backup[i]
This happens later and when it tries to do this it always tries to put it into element 11.
Does anyone have any ideas how I can ensure the returned data from putEntity is copied back into the correct element of the grid.data and grid.backup arrays?
You need to create a closure over i. What you can do is create a function
var updateGridData=function(entityType, entityId, gridDataToUpdate, gridIndex)
entityService.putEntity(entityType, entityId,gridDataToUpdate)
.then(function (result) {
angular.copy(result, $scope.grid.data[gridIndex]);
angular.copy(result, $scope.grid.backup[gridIndex]);
}, function (result) {
alert("Error: " + result);
})
}
So your main method becomes
for (var i = 0, len = $scope.grid.data.length; i < len; i++) {
if (!angular.equals($scope.grid.data[i], $scope.grid.backup[i])) {
var rowData = $scope.grid.data[i]
var idColumn = $scope.entityType.toLowerCase() + 'Id';
var entityId = rowData[idColumn];
updateGridData($scope.entityType, entityId, $scope.grid.data[i],i);
}
}
You can get some more idea from this question JavaScript closure inside loops – simple practical example

Categories

Resources