Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create a pointer to an unsaved ParseObject

I am having troubles referring to a "User" object from inside a query. I have the following code:

Parse.Cloud.define("getTabsBadges", function(request, response) {
  var UserObject = Parse.Object.extend('User');
  var user = new UserObject();
  user.id = request.params.userId;


  // Count all the locked messages sent to the user
  var receivedMessagesQuery = new Parse.Query('Message');
  receivedMessagesQuery.equalTo('status', 'L');
  receivedMessagesQuery.equalTo('toUser', user); // THIS LINE GENERATES THE ERROR


  receivedMessagesQuery.count({
    // more code here
  });
});

I call the function using CURL but I always get the following error:

{"code":141,"error":"Error: Cannot create a pointer to an unsaved 
ParseObject\n    at n.value (Parse.js:14:4389)\n    at n 
(Parse.js:16:1219)\n    at r.default (Parse.js:16:2422)\n    at e.a.value 
(Parse.js:15:1931)\n    at main.js:9:25"}

I am using the exactly same code in another project, the only difference is that instead of counting objects I find them and its works correctly. I have also verified that the tables have a column type of Pointer<_User> in both projects. What's causing the problem?

like image 941
Umar Jamil Avatar asked Oct 18 '22 23:10

Umar Jamil


1 Answers

The error message Cannot create a pointer to an unsaved means that you are trying to use an object which does not exist in the Parse DB.

With var user = new UserObject();, you're creating a new user object. You cannot use it in a query until you save it to Parse.

Instead of creating a new User object and setting it's objectId, do a query for the User object. See code below:

Parse.Cloud.define("getTabsBadges", function(request, response) {
    var UserObject = Parse.Object.extend('User');
    var query = new Parse.Query(UserObject);
    query.get(request.params.userId, {
        success: function(user) {
            // Count all the locked messages sent to the user
            var receivedMessagesQuery = new Parse.Query('Message');
            receivedMessagesQuery.equalTo('status', 'L');
            receivedMessagesQuery.equalTo('toUser', user); // THIS LINE GENERATES THE ERROR

            receivedMessagesQuery.count({
                // more code here
            });
        },
        error: function(error) {
            // error fetching your user object
        }
    });
});
like image 110
kRiZ Avatar answered Oct 21 '22 17:10

kRiZ