javascriptparse-platformparse-cloud-codeafter-save

Parse.com Cloud Code afterSave( ) Error: "Uncaught Tried to save an object with a pointer to a new, unsaved object."


Context

I am using Parse.com after my backend for a web application. The problem I am having relates to Cloud Code.

After a user completes their profile, I want to call an afterSave( ) function which updates a few fields in the dataset MY_TABLE.

The fields being updated (variable01, variable02, and variable03) are retrieved via the function MY_FUNCTION with inputs param01 and param02.

Error

I get the following error, which I cannot fix: "Uncaught Tried to save an object with a pointer to a new, unsaved object."

Code

Parse.Cloud.afterSave("PROFILE_COMPLETE", function(request) {
    // Use Master Key
    Parse.Cloud.useMasterKey();

    // Identify Parameters in Request
    var param01 = request.user.get('param01');
    var param02 = request.user.get('param02');

    // Print Parameters to Console
    console.log("Parameter 01 (" + param01 + ") successfully identified");
    console.log("Parameter 02 (" + param02 + ") successfully identified");

    // Identify Variables by Calling MY_FUNCTION()
    Parse.Cloud.run('MY_FUNCTION', {param01: param01, param02: param02}, {
        success: function(results) {
            // Print Results
            console.log("Function MY_FUNCTION() returned successful response");
            console.log(results);

            // Print Result Variables
            console.log("Identified variable01 = " + results.variable01);
            console.log("Identified variable02 = " + results.variable02);
            console.log("Identified variable03 = " + results.variable03);

            // Do I need to extend MY_TABLE or something else here???
            // I am doing the update and save wrong...

            // Update 'Complete' Data
            request.object.set("variable01", results.variable01);
            request.object.set("variable02", results.variable02);
            request.object.set("variable03", results.variable03);

            // Save Variables to 'MY_TABLE'
            request.object.save(null, {
                success: function(results) {
                    console.log("Variables successfully updated in MY_TABLE dataset");
                    console.log(results);
                },
                error: function(error) {
                    console.log("Error when saving variables in MY_TABLE dataset");
                    console.log("Error " + error.code + ": " + error.message);
                }
            })
        },
        error: function(error) {
            console.log("There was an error when running function MY_FUNCTION()");
            console.log(error);
        }
    });
});

Solution

  • Probably missing some information in your description but I hope my previous experience can help:

    Last time I got this error, I was doing the following:

    context: inside a query callback where the result variable is res

    var MyParseClass = Parse.Object.extend("MyParseClass");
    var newObject = new MyParseClass();
    
    res.set("pointerFieldToMyParseClass",newObject);
    
    res.save();
    

    In this example, newObject doesn't have an existence outside the scope of this function, so you must save it first:

    var MyParseClass = Parse.Object.extend("MyParseClass");
    var newObject = new MyParseClass();
    
    newObject.save({
       success:function(savedObject){
    
          res.set("pointerFieldToMyParseClass",newObject);
          res.save();
    
       },error:function(e){}});