Title: Javascript PFCloud for Objective C
Tags: javascript;parse-platform
Question: With the Javascript code below, do I need to define the name of my Parse database (_User) I want it to access? At the moment I'm getting 'error code: 141', so I thought maybe it doesn't know what database I want it to query, since I have more than one.
Parse.Cloud.define("test", function(request, response) {
Parse.Cloud.useMasterKey();
var friendRequestId = request.params.friendRequest;
var query = new Parse.Query("FriendRequest");
query.get(friendRequestId, {
success: function() {
response.success("Success!");
},
error: function(error) {
response.error(error);
For those who want to see where I call the PFCloud
[PFCloud callFunctionInBackground:@"test" withParameters:@{@"friendRequest" : friendRequest.objectId} block:^(id object, NSError *error) {
if (!error) {
NSLog(@"Success");
else {
NSLog(@"Error");
Thanks!

Here is the accepted answer: What do you mean, which database you want it to access?  You define the class you'd like to query new Parse.Query("class");  So you're querying through the FriendRequest class.  Perhaps your class name isn't actually FriendRequest?  It's case sensitive.
I recommend that you use Parse Promises rather than callbacks. Also, an alternative to querying for an object based on id is fetching the object based on id.  Here's my suggestion for what your cloud code should look like:
**Note, OP requested some additional help in the comments, so this answer is being updated to reflect his other questions.
Parse.Cloud.define("test", function( request, response )
Parse.Cloud.useMasterKey() //You only need this if you're, say, editing a user that didn't make this request or accessing an object that users don't have access to
var FriendRequest = Parse.Object.extend("FriendRequeset");
var friendRequestId = request.params.friendRequest;
var friendRequest = new FriendRequest();
var user1;
var user2;
friendRequest.id = friendRequestId;
friendRequest.fetch().then
function( friendRequest )
{
user1 = friendRequest.get("user1");
user2 = friendRequest.get("user2");
return Parse.Object.fetchAll([user1, user2]);
},
function( error )
{
response.error("Error fetching FriendRequest: " + error.message); //This will give you a bit more of a description of your errors
}
).then //fetchAll
function( objects )
{
//set up your relations. I've never used relations, but I'm sure you can find some examples
return Parse.Object.saveAll([user1, user2]);
},
function( error )
{
response.error("Error fetching users: " + error.message);
}
).then //saveAll
function( objects )
{
response.success("Success!");
},
function( error )
{
response.error("Error saving the users: " + error.message);
}
);
Notice I added some output to your error call.  That'll help you a lot while debugging down the road. Get used to doing that.
And finally, check your logs from your Parse dashboard. It usually tells you what line you hit an error on. Look for the "on main.js: xxxx:yy" the xxxx is your line number where the error occurred.
edit - I saw the NSLogs from your obj-c code and mistook those for your response.success/error calls from cloud code.  If you saw this before I edited it, I totally lied about that probably being your error :p
Comment for this answer: Legend. Cheers cobba.
Comment for this answer: Hey Jake. I thought I'd ask: is it possible to create a relation inside the query you have written above, but for a different database? So query the "FriendRequest" database, retrieve the data and then from that create a relation for a user in the "_User" database? Thank you!
Comment for this answer: Basically, when User1 sends User2 a friend request, in the FriendRequests database, User1 gets stored in the "From" column and User2 gets stored in "To". Now the JS Cloud code I have finds any requests the current user has, and when the request get's accept by the current user (User2) I'm trying to get it to create a relation for User1 in the "Friends" column with User2. But the problem is, the User's are stored in the "_User" database, not the "FriendRequests" database that I am querying. I hope that makes sense
Comment for this answer: Thanks for this Jake, I appreciate it. The way I create the relation is `var relation = User1.relation("friends");` and then `relation.add(User2);`. I just cant create this relation for User1 since the "friends" column is in the database "_User" and not the "FriendRequest" database which this code is querying. Is this possible to do? I've tried hiring freelancer programmers to do this but they have no idea what I'm trying to do. It seems like it shouldn't be too hard though. :(
Comment for this answer: Never mind, In my Objective C code I'm now passing the "To" user's objectId and the "From" user's objectId in a mutable dictionary as the parameter (using PFCloud:CallFunctionInBackground:WithParameters..), So I don't need to query the "FriendRequest" database anymore, I can just query the "_User" database in the Cloud Code and create the relation using the objectId's, I hope.
Comment for this answer: Thanks for your help man, I appreciate it. If you have a freelancer.com account I could award you $45 AUD if you wrote a working script for me? If not then its all good! Cheers.
Comment for this answer: Thanks for marking me as correct. Hope you got your issue solved.
I forgot to mention, to help with basic debugging down the road.
Use console.log("message"); to output to the debugging console.  You can throw a Parse.Object in there with no quotes and it will output the object and all of it's properties that are loaded.
Comment for this answer: Look at the examples in the javascript guide.  You don't generally create a relation out of nowhere, you access the relation stored on an object and then modify it.  So assuming that the FriendRequest somehow helps you access the _User you're looking for, then yes, you can use the relation.
What exactly are you trying to make a relation for? Which object is it stored on, and which objects should it reference?
Comment for this answer: If you have a pointer to the users stored on your FriendRequest, you can store it in a variable then fetch it and perform whatever operation on it that you need to.
I'll update my code a little bit for you.
Comment for this answer: If To and From are pointers to users on the FriendRequest object, you could go back to using the query.get() method, but before performing the get, do query.include("To"); and query.include("From"), which will have those objects fetched along with the object returned by the query. This will save you the most API calls, and allow you to chain fewer promise call backs.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:08/03/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><parse-platform>', 'Title': 'Javascript PFCloud for Objective C', 'ViewCount': 186, 'id': 31798211, 'user_AboutMe': '', 'user_AccountId': 6487107.0, 'user_DisplayName': 'CrackerBarrelKid55', 'user_Reputation': 549.0, 'user_Views': 137.0, 'user_id': 5022317.0}}