Cannot create a pointer to an unsaved ParseObject

此生再无相见时 提交于 2019-12-12 14:28:59

问题


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?


回答1:


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
        }
    });
});


来源:https://stackoverflow.com/questions/33342569/cannot-create-a-pointer-to-an-unsaved-parseobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!