Search for case insensitive data from Parse using javascript - 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++;
}
}
}

Related

How do I query parse for this?

so I am passing a sender Id which is the same as a parse objectId. My whole idea is that I am trying to match up messages with appropriate profile pictures.
I am trying to go to my user object and find the line in object id that === to my senderId and then I need to return the picture attribute value from that row. object.get(picture) I presume? is this right? I am not getting the appropriate output. picture and objectId are both attributes of my table.
//var User = Parse.Object.extend("User");
var query = new Parse.Query(Parse._User);
query.equalTo("objectId", senderId);
query.find({
success: function(object) {
var senderPicture = object.get('picture');
$('<img src="'+ senderPicture +'">').load(function() {
$(this).width("150").height("150").appendTo('#MenuBackground');
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});

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.

Parse.com fetch collection

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.

Categories

Resources