How can I get object ID of the Parse Object in Cloud Code?

1,322 views
Skip to first unread message

duong...@gmail.com

unread,
Apr 6, 2016, 3:51:03 AM4/6/16
to back{4}app
Hello, 
I need to get objectID of an Parse Object.
In the old parse, I get the object ID via statement: object.id, but in this host I can't get it.

My example code:

like.save(null).then(function(result) {
var createdMemeID = result.get("created_meme").id;
var MeMe = Parse.Object.extend("MeMe");
var query = new Parse.Query(MeMe);
query.equalTo("objectId", createdMemeID);
query.include("created_user");
return query.first();
}).then(function(result) {
var createdUser = result.get("created_user");
toUserDeviceToken = createdUser.get("deviceToken");
toUserId = createdUser.id;


Davi Macêdo

unread,
Apr 7, 2016, 2:55:41 AM4/7/16
to back{4}app
Hi, Try just:

duong...@gmail.com

unread,
Apr 7, 2016, 11:59:06 AM4/7/16
to back{4}app
But, I want to get the ObjectID of a pointer in the result, not the ObjectID of the result.

My code is like : 

comment.save(null).then(function(result) {
var meme = result.get("created_meme");
response.success(meme.id);

I want get objectID of meme via meme.id, but I can't.
I also try:

result.get("created_meme").id

but I can't get the id although I can get it in Parse.com

How to fix it? I use ObjectID everywhere so I need to fix it. Thank you. :)

Davi Macêdo

unread,
Apr 8, 2016, 12:51:29 PM4/8/16
to back{4}app
When you save, the result is already the object you've just saved. Just do it:
comment.save().then(function(result) {
var meme = result;
response.success(meme.id);

duong...@gmail.com

unread,
Apr 10, 2016, 2:14:47 AM4/10/16
to back{4}app
I am sorry :(, but I need to get the ID of Pointer, not the ID of result.

There are something I can explain to you:

1. My cloud code function:

Parse.Cloud.define("postAComment", function(request, response) {
//Use master key
Parse.Cloud.useMasterKey();
//Params from request
var nameOfUser = request.params.name_of_user;
var userId = request.params.created_user; // User who liked this meme post
var memeId = request.params.created_meme; // User who
var content = request.params.content; 
// //Create two pointer
var meme = {"__type": "Pointer",
"className": "MeMe",
"objectId": memeId
};
var user =  {"__type": "Pointer",
"className": "_User",
"objectId": userId
};
//Comment
var Comment = Parse.Object.extend("Comment");
var comment = new Comment();
//Set variable to Object Comment
comment.set("created_meme", meme);
comment.set("created_user", user);
comment.set("content", content);
//Comment save
comment.save(null).then(function(result) {
response.success(result);
}
});


2. The result of this response is:

{
  "result": {
    "created_meme": {
      "__type": "Pointer",
      "className": "MeMe",
      "objectId": "Qg8hcLsJKA"
    },
    "created_user": {
      "__type": "Pointer",
      "className": "_User",
      "objectId": "Z1haCpo00L"
    },
    "content": "Hello",
    "createdAt": "2016-04-10T06:00:17.209Z",
    "updatedAt": "2016-04-10T06:00:17.209Z",
    "objectId": "wYm7NVuKVa",
    "__type": "Object",
    "className": "Comment"
  }
}

3. And when I want to get the ObjectID of pointer named created_meme, I change this code from:

comment.save(null).then(function(result) {
response.success(result);
}

to...

comment.save(null).then(function(result) {
var meme = result.get("created_meme");
response.success(meme.id);
}

The response which I got is:
{}

I think the result must be:

{
result: "Qg8hcLsJKA";
}

And I check the log, no error was happen...
I can get the objectID of the pointer using this method which I explain above via Parse.com
But I can't get it in your service... :(





Davi Macêdo

unread,
Apr 10, 2016, 3:51:42 PM4/10/16
to back{4}app
Hi, I created an app and simulated your scenario. The code below worked for me:

Parse.Cloud.define("postAComment", function(request, response) {
//Use master key
Parse.Cloud.useMasterKey();
//Params from request
var nameOfUser = request.params.name_of_user;
var userId = request.params.created_user; // User who liked this meme post
var memeId = request.params.created_meme; // User who
var content = request.params.content; 
// //Create two pointer
var meme = {"__type": "Pointer",
"className": "MeMe",
"objectId": memeId
};
var user =  {"__type": "Pointer",
"className": "_User",
"objectId": userId
};
//Comment
var Comment = Parse.Object.extend("Comment");
var comment = new Comment();
//Set variable to Object Comment
comment.set("created_meme", meme);
comment.set("created_user", user);
comment.set("content", content);
//Comment save
comment.save(null).then(function(result) {
response.success(result.get("created_meme").objectId);
});
});

duong...@gmail.com

unread,
Apr 10, 2016, 10:01:24 PM4/10/16
to back{4}app
This code is worked. Thank you every much.
I am so impressed with you great help.
Thank you again... :D 

Davi Macêdo

unread,
Apr 10, 2016, 11:21:02 PM4/10/16
to back{4}app
You're welcome! :)
Reply all
Reply to author
Forward
0 new messages