Hamza L. Hamza L. - 3 months ago 13
Javascript Question

Parse - unable fetch and update column from Installation class

I am trying to fetch data from

Parse.Installation
class using the constraint
query.equalTo("installationId", uuid);
then, when I want to update the rows that I got in the success callback; however, I was no able to do so, I don't receive any errors so far.

I am executing it from the cloud code.

Here is the code:

Parse.Cloud.define("updateInstallationClass", function(request, response) {

var uuid = request.params.uuid;
var userID = request.params.userID;
var GameScore = Parse.Object.extend(Parse.Installation);
var query = new Parse.Query(GameScore);
query.equalTo("installationId", uuid);
query.first({useMasterKey:true},{
success: function(object22) {
// Successfully retrieved the object.
object22.set("userID3", userID);
object22.save(null, {useMasterKey:true});
console.log("userID3", userID);
console.log('success ', object22);
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
});

Answer

If you want to update Parse.Installation entries you must call the following function before ParseObject.save():

Parse.Cloud.useMasterKey();

Result code:

Parse.Cloud.define("updateInstallationClass", function(request, response) {

    var uuid = request.params.uuid;
    var userID = request.params.userID;
    var GameScore = Parse.Object.extend(Parse.Installation);

    var query = new Parse.Query(GameScore);
    query.equalTo("installationId", uuid);
    query.first({useMasterKey:true},{
        success: function(object22) {
            // Successfully retrieved the object.

            // >>>>>>>>>>>>>>>>>>>>>>>>>
            Parse.Cloud.useMasterKey();
            // <<<<<<<<<<<<<<<<<<<<<<<<<

            object22.set("userID3", userID);
            object22.save(null, {useMasterKey:true});
            console.log("userID3", userID);
            console.log('success ', object22);
        },
        error: function(error) {
            alert("Error: " + error.code + " " + error.message);
        }
    });
});

Edit 1:

Call Parse.Cloud.useMasterKey(); at the first line, Before execute the query. Your installation table has no read permission.

Parse.Cloud.define("updateInstallationClass", function(request, response) {

    // >>>>>>>>>>>>>>>>>>>>>>>>>
    Parse.Cloud.useMasterKey();
    // <<<<<<<<<<<<<<<<<<<<<<<<<

    var uuid = request.params.uuid;
    var userID = request.params.userID;
    var GameScore = Parse.Object.extend(Parse.Installation);

    var query = new Parse.Query(GameScore);
    query.equalTo("installationId", uuid);
    query.first({useMasterKey:true},{
        success: function(object22) {
            // Successfully retrieved the object.

            object22.set("userID3", userID);
            object22.save(null, {useMasterKey:true});
            console.log("userID3", userID);
            console.log('success ', object22);
        },
        error: function(error) {
            alert("Error: " + error.code + " " + error.message);
        }
    });
});

Edit 2:

To save a pointer values, you must create new instance of the class and assign the objectId.

Parse.Cloud.define("updateInstallationClass", function(request, response) {

    // >>>>>>>>>>>>>>>>>>>>>>>>>
    Parse.Cloud.useMasterKey();
    // <<<<<<<<<<<<<<<<<<<<<<<<<

    var uuid = request.params.uuid;
    var userID = request.params.userID;
    var GameScore = Parse.Object.extend(Parse.Installation);

    var query = new Parse.Query(GameScore);
    query.equalTo("installationId", uuid);
    query.first({useMasterKey:true},{
        success: function(object22) {
            // Successfully retrieved the object.

            var user = new Parse.User();
            user.set("objectId", userID);
            object22.set("userID3", user);

            object22.save(null, {useMasterKey:true});
            console.log("userID3", userID);
            console.log('success ', object22);
        },
        error: function(error) {
            alert("Error: " + error.code + " " + error.message);
        }
    });
});

Edit 3:

Replace:

var user = new Parse.User();
user.set("objectId", userID);

With:

var UserObj = Parse.Object.extend("User");
var user = new UserObj();
user.id = userID;
Comments