Parse.com fetch collection - javascript

I'm new to Parse.com, and I want to ask, how display all items from collection. This is my code:
var User = Parse.Object.extend("User");
var TestCollection = Parse.Collection.extend({
model: User
});
var collection = new TestCollection();
collection.fetch({
reset: false,
success: function(collection) {
document.write('<h1>' + "Users:<br>" + '</h1>');
document.write('<table><tr>');
document.write('<td>User name</td>');
document.write('<td>Email</td></tr>');
collection.each(function(user) {
document.write('<tr><td>'+user.get('username')+'</td>');
document.write('<td>'+user.get('email')+'</td>');
document.write('</tr></table>');
});
},
error: function(collection, error) {
alert("Error: " + error.code + " " + error.message);
}
});
In this way, I got all items, but the webpage always reload without stop. Help please. Thanks in advance!

Once you've fixed up the document.write() issues, you'll also need to be aware that the User (and Role and Installation) classes are special in Parse.
To query them, you can't do it the way you have. Delete the line where you're extending with "User" as that won't work.
Change your collection to the following:
var TestCollection = Parse.Collection.extend({
model: Parse.User
});
The way you have done it is correct for any classes you create, but you have to use the inbuilt definitions for the inbuilt Parse classes.

Related

Parse Javascript - Adding user error

I created a factory in angularJS to store data to Parse User object. Every time I click to process the details and open an account for the user, it shows the following error in console:
Failed to load resource: https://api.parse.com/1/users the server
responded with a status of 400 (Bad Request)
I checked if the service parameter is valid with attributes content, and yes it has content (not empty). This is the service body:
.factory('CreateUserService', function() {
return {
createAccount: function(user) {
console.log("Service User: " + user.name);
var parseUser = new Parse.User();
parseUser.set("username", user.username);
parseUser.set("email", user.username);
parseUser.set("name", user.username);
parseUser.set("password", user.username);
parseUser.set("mobile", user.username);
/*Attempt to create user in DB*/
parseUser.signUp(null, {
success: function(parseUser) {
// Hooray! Let them use the app now.
//alert("success!");
return 1;
},
error: function(parseUser, error) {
// Show the error message somewhere and let the user try again.
//alert("Error: " + error.code + " " + error.message);
return error;
}
});
}
}
});
I added the Parse CDN in the index.html of my application with:
<script src="http://www.parsecdn.com/js/parse-1.6.7.min.js"></script>
Update: In Parse API Guide, this is the body of the signup:
user.signUp(null, {
success: function(user) {
// Hooray! Let them use the app now.
},
error: function(user, error) {
// Show the error message somewhere and let the user try again.
alert("Error: " + error.code + " " + error.message);
}
});
The user object in Parse (I review through the browser) is not showing any changes to the rows number hence user not added. Any idea what is going wrong?
Thanks
Update: When I deleted the existing empty user object in Parse and ran the code, it worked and created a user Object with all columns specified in the code. Then if I try to add another user, I get a POST error. There must be a way around this since signup method by parse surely considered adding multiple users to the User object.

Parse.com Js Sdk + Angular pointers

What if I have an array of events that I want users to be able to rsvp for? Essentially needing the data of the "user" who clicked "rsvp", and the "title" of the "event" that they've rsvp'd for. Could I make a pointer in a Agree class that includes the user's name/id and another pointer that includes the title of the event rsvp'd for? Is there a way to somehow use Angular to add code to the "Agree" class with a form?
User Class:
objectId
username
password
Agree Class:
objectId
username-(current user)
Comments Structure:
Event Class:
objectId-
title-(Need this title)
description-
date-
Please help me understand how to make this work..Thanks!
$scope.getAgree = function(){
var Agree = Parse.Object.extened("Agree");
var query = new Parse.Query("Agree");
query.include("userName");
query.find().then(function(results){
//Go through each in Array
var rsvpOjectArray = new Array();
or(i in results){
//Set Object to current RsvpObject
var obj = results[i];
//Get user
var userName = obj.get("userName").get("username");
rsvpOjectArray.push({
user: {userName
}
});
}
});
};
$scope.makeAgree = function(){
var Agree = Parse.Object.extend("Agree");
var agree = new Agree();
var user = new Parse.Object("Agree");
agree.set("userName", Parse.User.current());
agree.save(null, {
success: function(rsvp) {
// Hooray! Let them use the app now.
alert("success!");
},
error: function(rsvp, error) {
// Show the error message somewhere and let the rsvp try again.
alert("Error: " + error.code + " " + error.message);
}
});
};

{"code":101,"error":"object not found for update"} Parse.com Javascript SDK

I'm trying to update certain column value of my class matching the objectId of the class , Here is my code below
var GameScore = Parse.Object.extend("Address");
var query = new Parse.Query(GameScore);
query.equalTo("objectId", "xxxx");
query.first({
success: function(object) {
alert(JSON.stringify(object));
object.set("address","kkkkkk");
object.save();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
Now in the success function i get the row data for the given objectId, But the object.set and object.save alone not working , please advice what is gone wrong here.
I get "{"code":101,"error":"object not found for update"}" error in the reponse.
Thanks in advance :)
Check both class level permission and object level(ACL) permission and make sure client have permission to update the object.
Also note that, All class and object have their own permission.
var acl = new Parse.ACL();
acl.setPublicWriteAccess(true);
acl.setPublicReadAccess(true);
acl.setWriteAccess(user.id, true);
model.setACL(acl);
Is a problem with ACLs. With this you can solve it

Using Parse Javascript SDK Query GeoPoint withinMiles

I am creating a Parse App and I want to be able to get all objects within a certain distance of a single object using GeoPoints. Seems simple, but the following code returns [] (No matches):
app.get('/photos', function(req, res) {
var Photo = Parse.Object.extend("Photo");
var query = new Parse.Query(Photo);
query.equalTo("owner", Parse.User.current());
query.find({
success: function(results) {
// Do something with the returned Parse.Object values
//res.send(results);
var photo = results[0];
// Create a query for places
var Photo = Parse.Object.extend("Photo");
var query = new Parse.Query(Photo);
// Interested in photos taken near this photo.
query.withinMiles("coordinates", photo.coordinates, 500000);
// Final list of objects
query.find({
success: function(photoObjects) {
res.send(photoObjects);
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
Oddly, if I change the line
query.withinMiles("coordinates", photo.coordinates, 500000);
to
query.near("coordinates", photo.coordinates);
then it works without a hitch and returns all photos (Every single one has a GeoPoint within about a 5 mile radius of all the others in the sample data set, so I used a maxDistance of 500000 to test the extent of the issue).
The problem is I need to be able to limit to a specific radius, which is what I thought the "withinMiles" was doing. Any ideas what I may be doing wrong?
In case someone is finding this and shares my troubles, the following was the issue:
I was assuming that all "columns" of data objects in parse are treated as attributes. i.e. - You can access them using the . operator (photo.coordinates).
In fact, you must access them via photo.get("coordinates") to get the actual objects contained within the data structure.
Not sure why the above scenario was working in the case of the "near" function, but nonetheless the behavior works correctly when I started using the get accessor method for the Parse data objects.

Search for case insensitive data from Parse using javascript

I am using Parse.com and access it using Javascript. I want to search data in such a manner that it will not search for case sensitive like if i search abc then it will give abc, Abc, ABC,aBc etc all. but currently it gives only abc.
JavaScript:
var search_data="Temp";
var product = Parse.Object.extend("product");
var query = new Parse.Query(product);
query.contains("name",search_data);
query.find({
success: function(results) {
console.log("Total Product Found : "+results.length);
printSearchProducts(results,query); //custom method to print product detail
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
}
});
Actual Result: Temp1,Temp2,Temp3,ProductTemp3,Temp4Product etc.
Required Result: Temp1,Temp2,Temp3,ProductTemp3,Temp4Product,temp5,producttemp6,TEMP7,tEMp8 etc.
For that you can use Parse.Query's matches() function, which even it's not indicated in its documentation page, but you can use it like this :
query.matches(key, value, 'i');
Hope that can help.
At this point in time you aren't able to perform case insensitive searches via a query.
A very easy workaround for this however, is to store a lower case version of the field you need to do this query on.
Creating The Item
var Product = Parse.Object.extend("product");
var newProduct = new Product();
var productName = "Temp4Product";
newProduct.set("name",productName);
newProduct.set("name_lowercase",productName.toLowerCase());
newProduct.save();
Performing the query
var search_data="Temp";
var product = Parse.Object.extend("product");
var query = new Parse.Query(product);
query.contains("name_lowercase",search_data.toLowerCase());
query.find({
success: function(results) {
console.log("Total Product Found : "+results.length);
printSearchProducts(results,query); //custom method to print product detail
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
}
});
Note in the above example I've used the string.toLowerCase() function, however there may be a more appropriate function for you to use. Basically you want to find a way to "simplify" your data so that you can perform the appropriate query.
The solutions that suggest an extra stored field with the lowercase value have the disadvantage of requiring additional storage. The matches method works, but has a minor performance cost.
A better workaround that avoids both of these issues is to use an aggregate with the $toLower expression in a projection.
I have solved this issue.
I remove constraint query.contains("name",search_data); and apply manual search using java script inside printSearchProducts method like:
var search_data="Temp";
var product = Parse.Object.extend("product");
var query = new Parse.Query(product);
query.limit(500);
query.find({
success: function(results) {
printSearchProducts(results,query,search_data);
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
}
});
printSearchProducts Method Where search manually via Java Script.
function printSearchProducts(results,query,search_data) {
var counter = results.length;
var productName = '',i=0;
search_data = search_data.toLowerCase();
if(counter){
while(i<counter){
var found = -1;
productName = results[i].get("name");
var lower_name = productName.replace(/[^a-zA-Z]/g,'_').toLowerCase();
found = lower_name.indexOf(search_data);
if(found>=0) {
//Access or print the required result here
}
i++;
}
}
}

Categories

Resources