5

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?

goodolddays
  • 2,595
  • 4
  • 34
  • 51
  • I think the only problem is that _User pointer not equal User pointer. Do you want to use User class, not _User (default class)? try var user = new Parse.User(); – ChunTingLin May 13 '16 at 04:52

2 Answers2

3

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
        }
    });
});
Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
kRiZ
  • 2,320
  • 4
  • 28
  • 39
  • 1
    What if I have array of user object ids and i want to create its pointers in loop? – HashT Dec 23 '15 at 08:29
  • 2
    @HashamTahir You could use the `createWithoutData( id ) ` method to create objects with only their `objectId`s in a loop. Within the loop, you could fetch the related data but the fetch operations may complete way after the loop ends. Javascript promises may help to solve the possible race condition. – kRiZ Jan 08 '16 at 00:13
0

Your request.params.userId may be undefined or null, which causes this error.

Your query constraints cannot compare the Parse Object that is created without data (createWithoutData()) using undefined or null as its objectId.

hsuchengmao
  • 524
  • 1
  • 6
  • 14