Taylorsuk Taylorsuk - 6 months ago 68
Node.js Question

Parse Server Cloud Code - Update OR Create object

I am using Parse Server on AWS and mLab with great success, except for my Cloud Code. The main issue is surrounding my previous code for Create OR Update an object. I used to do this by querying for a user pointer on the Favourites class. If a row contains a user pointer then I need to update its content, if it doesn't exist a row needs to be created.

Old Parse.com Code

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

console.log(request.params.favourites);

var Favourites = Parse.Object.extend("Favourites");
var query = new Parse.Query("Favourites");

query.equalTo('user', request.user);
query.first({
success: function(results) {

console.log(JSON.stringify(results));

console.log(results)
if (results === undefined) {


var favourites = new Favourites();

favourites.save({
user: request.user,
favourites: request.params.favourites
}, {
success: function(favourites) {
// The object was saved successfully.
},
error: function(favourites, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
}
});

} else {

results.set("favourites", request.params.favourites);
results.set("userId", request.user.id);
results.save();
}

response.success(results);
},
error: function(error) {
error.message("favourites lookup failed");
}
});
});


New Parse Server Code

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

console.log('user is : ' + JSON.stringify(request.user));

var Favourites = Parse.Object.extend("Favourites");
var query = new Parse.Query("Favourites");

query.equalTo("user", request.user);
query.first({
useMasterKey: true
}, {
success: function(results) {

if (results && results.length > 0) {

console.log('running found');
favourites.set("favourites", request.params.favourites);
favourites.set("userId", request.user.id);
favourites.save();
response.success();

} else {

var favourites = new Favourites();

favourites.set("user", request.user);
favourites.set("favourites", request.params.favourites);
favourites.set("userId", request.user.id);
favourites.save();
response.success();
}
},
error: function(error) {
console.log(error.message);
}
});
});

Answer

Do not response unless callback finished. Set response.error on each Parse requests error.

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

    console.log(request.params.favourites);

    var Favourites = Parse.Object.extend("Favourites");
    var query = new Parse.Query("Favourites");

    query.equalTo('user', request.user);
    query.first({
        //is this query need masterKey?
        useMasterKey: true,
        success: function(results) {

            console.log(JSON.stringify(results));

            console.log(results)
            if (results === undefined) {


                var favourites = new Favourites();

                favourites.save({
                    user: request.user,
                    favourites: request.params.favourites
                }, {
                    success: function(favourites) {
                        // The object was saved successfully.
                        response.success(results);
                    },
                    error: function(favourites, error) {
                        // The save failed.
                        // error is a Parse.Error with an error code and message.
                        response.error(error);
                    }
                });

            } else {

                results.set("favourites", request.params.favourites);
                results.set("userId", request.user.id);
                results.save(null, { useMasterKey: true }).then(response.success, response.error);
            }


        },
        error: function(error) {
            error.message("favourites lookup failed");
            response.error(error);
        }
    });
});
Comments