1
votes

In ACL of an object 'Hotel' i am trying to save id of 'Parse.User' object. I am saving ACL in beforeSave(). While saving object it gives me an error as "Uncaught Tried to save an object with a pointer to a new, unsaved object" Which i am unable to resolve.

Assign User function:

exports.assignUser = function(params) {
    if(!params || !params.userId || !params.id) {
        console.log(Response.ParametersEmpty);
        params.error(Response.EmptyParameters);
    } else {
        var Hotel = Parse.Object.extend('Hotel');
        var dsQuery = new Parse.Query(Hotel);
        dsQuery.equalTo('objectId', params.id);
        dsQuery.first({
            success: function(_hotel) {
                if(_hotel) {                                                                
                        _hotel.set('tempAssignedUser', params.userId);
                        _hotel.add('users', params.userId);
                        _hotel.save(null, {
                            success: function(_hotel) {                                                         
                                params.success(_hotel); 
                            },
                            error: function(_hotel, error) {                                
                                params.error(Response.InternalServerError);
                            }
                        });                                            
                } else {
                    params.error(Response.NotFound);
                }
            },
            error: function(error) {
                params.error(Response.InternalServerError);
            }
        });
    }
};

beforeSave()

Parse.Cloud.beforeSave('Hotel', function(req, res) {
    var hotel = req.object;

    //CREATING AND SETTING ACLS TO Hotel
    var hotelACL = new Parse.ACL();   
    **if(hotel.get('tempAssignedUser')) {
        hotelACL.setWriteAccess(hotel.get('tempAssignedUser'), true);
        hotelACL.setReadAccess(hotel.get('tempAssignedUser'), true);    
        hotel.unset('tempAssignedUser');
    }** // If i remove this if condition, my code is working fine                                      
    hotelACL.setRoleWriteAccess('ADMIN', true);
    hotelACL.setRoleReadAccess('ADMIN', true);
    hotelACL.setRoleWriteAccess('SUPER_USER', true);
    hotelACL.setRoleReadAccess('SUPER_USER', true);
    hotelACL.setPublicReadAccess(true);

    hotel.setACL(hotelACL);
    res.success();
}
2
Basically it says that you are trying to save Hotel object with a pointer to User object, which isn't saved. So try save User object, and then save Hotel object. - Juri Noga
Thank you for your reply but user object is already saved and then i am assigning - Charmi Shah

2 Answers

0
votes

You are setting tempAssignedUser with a userId. That is NOT the same thing as setting the tempAssingedUser with a _User object. What you are doing is setting a placeholder, which is 'unsaved' regardless of whether the original object with that id is saved or not.

You need to first FETCH the tempAssignedUser with id params.userId and THEN set the property of Hotel with hotel.set("tempAssignedUser",fetchedUser)

0
votes

First make sure that hotel.get('tempAssignedUser') returns a valid objectId for a user that exists in your User class. Also I am assuming that there is a field called tempAssignedUser defined in your Hotel class. Now you can try this instead in your beforeSave:

Parse.Cloud.beforeSave('Hotel', function(req, res) {
    var hotel = req.object;

    var hotelACL = new Parse.ACL();
    hotelACL.setRoleWriteAccess('ADMIN', true);
    hotelACL.setRoleReadAccess('ADMIN', true);
    hotelACL.setRoleWriteAccess('SUPER_USER', true);
    hotelACL.setRoleReadAccess('SUPER_USER', true);
    hotelACL.setPublicReadAccess(true);

    if(hotel.get('tempAssignedUser')) {

        // fetch the user object
        Parse.Cloud.useMasterKey();
        var userObject = new Parse.User();
        userObject.id = hotel.get('tempAssignedUser');

        userObject.fetch().then( function(user) {

            hotelACL.setWriteAccess(user, true);
            hotelACL.setReadAccess(user, true);    
            hotel.unset('tempAssignedUser');
            hotel.setACL(hotelACL);
            res.success();

        }, function(error) {
             response.error(error);
        }); 

    } else {

        hotel.setACL(hotelACL);
        res.success();
    }
});