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?
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
        }
    });
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With