How do I query parse for this? - javascript

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);
}
});

Related

Parse.com JavaScript query error handling does not work

I'm playing around with the Parse backend and NodeJS but I have a problem right now and I am not able to figure it out. I have a get request like this.
app.get("/movie", function (req, res) {
var Review = Parse.Object.extend("Review");
var query = new Parse.Query(Review);
var movie = req.query.movie;
query.equalTo("movie", movie);
query.first({
success: function (movie) {
console.log("Success");
},
error: function (error) {
console.log("Error: " + error.code + " " + error.message);
}
});});
This works fine but the error handling kinda doesn't. If, for example, I change
query.equalTo("movie", movie);
to query.equalTo("movie", "xyz");
or query.equalTo("abc", "xyz");
which does not exist in my table, I'm still getting the success statement. And yes, I have restarted the server every time.
Update
I tried the same query using the iOS SDK(Swift) and here I get into the error case. I always get the same error though, which shouldn't be, but at least I get an error while in the JS sample I always get into the success case.
I believe query.first() will not error if it does not find a "first" object. You can check for (movie == null) on the success return.
Update:
Try writing it this way:
app.get("/movie", function (req, res) {
var movie = req.query.movie;
var query = new Parse.Query("Review");
query.equalTo("movie", movie);
query.first().then(function (movie) {
// Success
console.log("Success");
}, function (error) {
// Error
console.log("Error: " + error.code + " " + error.message);
});
});
query.find() always success even if there is no match data in your table/collections
Let's try it
query.find().then(function (movies) {
if(movies.length > 0){
console.log("success");
}else{
console.log("query success but no data found");
}
}, function (error) {
// Error
console.log("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

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++;
}
}
}

html5sql - Can't seem to connect to my DB

I'm using html5sql.com for doing html5 DB stuff :o)
- Really a great module...
However, I got stuck!
At my index.html/index.js I create my database and tables in it.
try {
html5sql.openDatabase("com.dynamicvenues.fairkeyMobile.db","Questionnaire",3*1024*1024);
html5sql.process(
[
"CREATE TABLE IF NOT EXISTS Questionnaire (uid INTEGER, json TEXT, hash TEXT);",
"CREATE TABLE IF NOT EXISTS Answers (id INTEGER PRIMARY KEY, visitor_id TEXT, json TEXT);"
],
function(){
console.log("Success Creating Tables");
},
function(error, statement){
console.error("Error: " + error.message + " when processing " + statement);
}
)
} catch(error) {
alert("Database create failed: "+error.message);
}
And further in the same page I populate one table with data:
jQuery.get(serverHttp+"json.php?exhibitorID="+exhibitorID, function(data){
var html = $(data).map(function() {
return $(this).html();
});
var jsonStr = html[0];
var exhibitorID = html[1];
var hashStr = html[2];
var SQL = "INSERT INTO Questionnaire (uid, json, hash) VALUES ("+exhibitorID+",'"+jsonStr+"','"+hashStr+"')";
try {
html5sql.process(SQL,
function(){
console.log('Inserted 1 row!');
},
function(){
console.error("Error: " + error.message + " when processing " + statement);
}
)
} catch(error) {
alert("Query failed: "+error);
}
Now, in a different page called questionnaire.html/questionnaire.js I'm trying to retrieve the data I stored in the table Questionnaire.
html5sql.process(
["SELECT * FROM Questionnaire;"],
function(transaction, results, rowsArray){
for(var i = 0; i < rowsArray.length; i++){
var uid = rowsArray[i].uid;
var json = rowsArray[i].json;
var hash = rowsArray[i].hash;
console.log("Retrieved rows: "+uid+" - "+json+" "+hash);
}
console.log("Done selecting data");
},
function(error, statement){
console.error(error.message+" Occured while processing: "+statement);
}
);
What am I doing wrong???
Regards,
Daniel
Solved! Inserted: html5sql.openDatabase("com.dynamicvenues.fairkeyMobile.db","Questionnaire",3*102‌​4*1024); Before html5sql.process() at questionnaire.js

Categories

Resources