Parse.com Js Sdk + Angular pointers - javascript

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

Related

Parse.Cloud.run not recognizing variables

I am trying to link my website to a Parse server using mongodb. It was running well, and saving so i could see in parse dashboard, and mlab. I attempted to add a new variable which resulted in me messing something up. When this get run it throws an error saying address is not defined on the line with Parse.Cloud.run.
document.getElementById("btn").addEventListener("click", function(){
var name = document.getElementById('inp1').value;
var address = document.getElementById('inp2').value;
var city = document.getElementById('inp3').value;
var state = document.getElementById('inp4').value;
var zipcode = document.getElementById('inp5').value;
var Apartment = Parse.Object.extend("Apartment");
var apartment = new Apartment();
apartment.set("name", name);
apartment.set("address", address);
apartment.set("city", city);
apartment.set("state",state);
apartment.set("zipcode", zipcode);
apartment.save(null, {
success: function(apartment) {
// Execute any logic that should take place after the
object is saved.
alert('New object created with objectId: ' + prod.id);
},
error: function(error) {
// Execute any logic that should take place if the save
fails.
// error is a Parse.Error with an error code and
description.
alert('Failed to create new object, with error code: ' +
error.description);
}
});
});
Parse.Cloud.run("registerApartment",{"name":name,"address":address,"city":
city, "state": state,"zipcode":zipcode,"map":map}, {
success: function(savedApartment){
console.log(savedApartment);
}, error: function(error){
console.log(error);
}
})
thank you ahead of time!
cloud code
Parse.initialize('#');
Parse.serverURL = 'http://localhost:1337/parse';
Parse.Cloud.define("registerApartment", function(req,res){
var params = request.params;
var name = params.name;
var address = params.address;
var city = params.city;
var state = params.state;
var zipcode = params.zipcode;
var Apartment = Parse.Object.extend("Apartment");
var apartment = new Apartment();
apartment.set("name", name);
apartment.set("address", address);
apartment.set("city", city);
apartment.set("state",state);
apartment.set("zipcode", zipcode);
apartment.set("map", map);
console.log(apartment);
apartment.save(null, {
success: function(savedApartment){
console.log("Succesfully saved bitch");
response.success(savedApartment);
}, error: function(error){
console.log(error);
response.error(error);
}
})
});
could you double check your server where you define the cloud code?
Your code should be something along this line:
Parse.Cloud.define("registerApartment", function(req,res){
---your codes---
});
Make sure that your Parse.Cloud.run() is calling what you defined back at your server.
Edit:
Try to use this code instead of your current one:
Parse.Cloud.run("registerApartment",{name:name,address:address,city:
city, state: state,zipcode:zipcode,map:map}, {
success: function(savedApartment){
console.log(savedApartment);
}, error: function(error){
console.log(error);
}
})
All i did was remove the quotes when parsing in a parameter. This should work fine :)

How to make a copy of a Parse Object in the Cloud?

I would like to have a copy if an existing Parse Object, and then make some edits and save it as a new Parse Object rather than setting each field manually.
Here is my cloud function :
Parse.Cloud.define("SharePost", function(request, response) {
var ShareUserID=request.params.ShareUserID;
var UserID=request.params.UserID;
var PostID=request.params.PostID;
Parse.Cloud.useMasterKey();
var user = new Parse.User({id:UserID});
var shareuser = new Parse.User({id:ShareUserID});
var query = new Parse.Query("Feed");
query.get(PostID, {
success: function(post) {
var Post = Parse.Object.extend("Feed");
var newpost = new Post()
// here I would like to get the same object and make some edits o, it
post.save( {
success:function () {
response.success("Success");
},
error:function (pointAward, error) {
response.success(error);
}
}
);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
});
There might be a prettier way, but one way that's sure to work without relying on any subtleties would be this:
function pfClone(fromObject, toObject, keys) {
var _ = require('underscore');
_.each(keys, function(key) {
toObject.set(key, fromObject.get(key));
});
}
call it like this:
// after fetching a post called "post"
var Post = Parse.Object.extend("Feed");
var newpost = new Post();
var keys = ["title", "author" /* ...the keys you want to copy unchanged */ ];
pfClone(post, newpost, keys);
// change other properties of newpost here
Even prettier would be a version that introspects on the passed object, and then builds and initializes the clone. The one inelegance for either of these ideas is that (last time I checked) PFObject doesn't let you introspect the keys, so you're stuck passing in an array of keys.

How to Parse values from some posts to a object id in a different class

I'm trying to make a connection of two classes in Parse.com via Pointer.
I have one class which is called magazia and I put some rows inside.
Also I have a class which is called "Events" which has a magaziaid column which is Pointer to class magazia.
I want to make a post in Events with a specific objectId of the other class ("magaziaid") from a form.
So I have this code for now:
function saveJobApp(objParseFile) {
var jobApplication = new Parse.Object("events");
var name = document.getElementById('name').value;
var description = document.getElementById('description').value;
var magaziid = "2xOhgyX0BU";
jobApplication.set("image", objParseFile);
jobApplication.set("name", name);
jobApplication.set("description", description);
jobApplication.set("magaziaid", this.magaziid.id); //breakpoint
jobApplication.save(null, {
success: function(gameScore) {
},
error: function(gameScore, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to create new object, with error code: ' + error.description);
}
});
//var objectId = jobApplication.getObjectId();
// objectId = document.getElementById("objID").innerHTML;
// console.log(objectId);
}
$('#submitId').on("click", function(e) {
var fileUploadControl = $("#profilePhotoFileUpload")[0];
var file = fileUploadControl.files[0];
var name = file.name; //This does *NOT* need to be a unique name
var parseFile = new Parse.File(name, file);
console.log("Done");
parseFile.save().then(
function() {
saveJobApp(parseFile);
},
function(error) {
alert("error");
}
);
});
});
That i'm trying to put the objectId copied from the "magazia" class, but i get an error, "Failed to create object with error code: Undefined"
What is my mistake here?
How can i pass the object id of a row from "magazia" class and put it on the pointer of the class "events" ???
Thanks in advance!
Update 1. in //breakpoint i have also tried those different codes
jobApplication.set("magaziaid", {__type: "Pointer", className: "events", objectId: magaziid});
and this one
jobApplication.set("magaziaid", magaziid);
Your pointer value should be a magazia object and not an id. Do the following and it should work.
var magaziaObject = new Parse.Object("magazia");
// add anything you want to magaziaObject
// like magaziaObject.id, magaziaObject.name, etc.
// and then save the magaziaObject in your events (jobApplication) object
jobApplication.set("magaziaid", magaziaObject);

Obtain Data from two parse classes based on column in one table

I have to Parse Classes in my data browser, 'Details' and 'Content'. The 'Details' class has the following --> 'objectId', 'uuid' and 'proximity'. The 'Content' class has 'objectId', 'descId' and 'offer'.
I have created a web UI using the Javascript SDK so when the user enters the uuid, proximity and offer, uuid and proximity get stored in the 'Details' class, on success I then get the objectId of the newly created object. I then store that objectId in the 'Content' class under descId and the offer that was inputted by the user.
My problem is I have a html table that I need to populate, so I need to pull the data from both classes. The uuid and proximity from 'Details' and the offer from 'Content' so I need to do this in one query. This is my reason for storing the 'Details' objectId in the 'Content' class as a type of foreign key.
I am stuck at this cross roads and have tried include etc but I am just trying things and I'm not sure what I need to do. If anyone can help, perhaps show me a sample, I'd greatly appreciate it
Here is my js save code:
//Creating Beacon Content Parse Object
var iBeaconContent = Parse.Object.extend("Content");
var beaconContent = new iBeaconContent();
//Creating Object to save to iBeacon Description Table
var iBeaconDescription = Parse.Object.extend("Details");
var beaconDescription = new iBeaconDescription();
beaconDescription.set("uuid", tdUuid.children("input[type=text]").val().toString());
beaconDescription.set("proximity", parseInt(tdProximity.children($('prox')).val().toString()));
beaconDescription.save(null, {
success: function(beaconDescriptionObject) {
var query = new Parse.Query("Details");
query.equalTo("uuid", tdUuid.children("input[type=text]").val().toString());
query.find({
success: function(results) {
objectId = results[0].id;
beaconContent.set("descId", objectId);
beaconContent.set("offer", tdOffer.children("input[type=text]").val().toString());
beaconContent.save(null, {
success: function(object) {
document.location.reload(true);
}, error: function(beaconContent, error) {
}
});
}
});
},
error: function(error) {
}
});
NEW JAVASCRIPT
var BeaconDetail = Parse.Object.extend("Details");
var BeaconContent = Parse.Object.extend("Content");
var innerQuery = new Parse.Query(BeaconDetail);
innerQuery.exists("descId");
var query = Parse.Query(BeaconDetail);
query.matchesQuery("objectId", innerQuery);
query.find({
success:function(beaconContent){
alert("Success----lenght: " + beaconContent.length);
}
})
Sound like you need to use a compound query or relationship query. Here are some links
https://docs.parseplatform.org/js/guide/#relational-queries
https://docs.parseplatform.org/js/guide/#compound-queries
https://parse.com/questions/compound-relational-queries
An example of a query from two classes is as follows
It would also be good to see the code, would help give a more relative answer.
CODE
var lotsOfWins = new Parse.Query("Player");
lotsOfWins.greaterThan("wins", 150);
var fewWins = new Parse.Query("Player");
fewWins.lessThan("wins", 5);
var mainQuery = Parse.Query.or(lotsOfWins, fewWins);
mainQuery.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.
}
});
If I understand correctly, your Content class contains a pointer to your Details class in the descId property, and you want to be able to query based on some Details fields and return both objects?
NOTE: I must point out that descId is a very poorly named property that will just cause confusion. If it is a pointer, just give it a name like desc, leave off the Id suffix.
Anyway, if that is what you want:
var query = new Parse.Query("Content");
var uuid = tdUuid.children("input[type=text]").val().toString();
var proximity = parseInt(tdProximity.children($('prox')).val().toString());
// consider logging those two variable to confirm they're what you want
// query properties of a pointer
query.equalTo("descId.uuid", uuid);
query.equalTo("descId.proximity", proximity);
// include the pointer in the output
query.include("descId");
query.find({
success: function(beaconContent) {
alert("Success -- length: " + beaconContent.length);
// sample output of first result:
var content = beaconContent[0];
var detail = content.get("descId");
console.log("uuid", detail.get("uuid"));
console.log("proximity", detail.get("proximity"));
console.log("offer", content.get("offer"));
}
});

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