imas145
imas145

Reputation: 1979

Parse Cloud function fails with error 141

What I'm trying to accomplish is when I call this cloud function (deploy), it will first delete all data from toClass, then iterate over objects in fromClass, copy and save them to toClass. When it saves an object, it will also delete it from fromClass. Simply put, move the objects from class to another. Calling this function on client

[PFCloud callFunctionInBackground:@"deploy" withParameters:@{@"toClass": kTilrClassUpdates, @"fromClass": kTilrClassPrototypeUpdates} block:^(id object, NSError *error) {
    if (error) {
        [self failed];
    } else {
        [self succeeded];
    }
}];

Will print this error message to client log: Error: undefined (Code: 141, Version: 1.2.19)

Here is the code for Parse Cloud:

Parse.Cloud.define("deploy", function(request, response) {
    var query = new Parse.Query(request.params.toClass);
    query.find({
        success: function(results) {
            for (var i = 0; i < results.length; ++i) {
                console.log(results[i]);
                results[i].destroy({
                    success: function(object) {

                    },
                    error: function(object, error) {
                        response.error(error);
                    }
                });
            }
        },
        error: function() {
            response.error(error);
        }
    });
    var query2 = new Parse.Query(request.params.fromClass);
    query2.find({
        success: function(results) {
            for (var index = 0; index < results.length; ++index) {
                var UpdateInfoClass = Parse.Object.extend(request.params.toClass);
                var updateInfo = new UpdateInfoClass();
                for (var k in results[index]) {
                    updateInfo.set(k, results[index][k]);
                }
                console.log(updateInfo);
                updateInfo.save(null, {
                    success: function(updateInfo) {
                        results[index].destroy();
                        if (index == results.length - 1) {
                            response.success();
                        }
                    },
                    error: function(updateInfo, error) {
                        response.error(error);
                    }
                });
            }
        },
        error: function() {
            response.error(error);
        }
    });
});

I really don't know much about JavaScript, so this could be a very simple mistake somewhere.

Upvotes: 0

Views: 795

Answers (1)

PetrV
PetrV

Reputation: 1368

There is couple of sections which can guarantee problems for you: While not questioning your design :

1) queries are not chained together and will run at the same

2) you cannot use

for (var k in results[index]) {
    updateInfo.set(k, results[index][k]);
}

to copy properties from one object to another. You need to call JSON.stringify(results[index]) to get standard array. You should set values by likes of request.object.set(fiedlName, value) .

3) "index" will not get propagated correctly into subblock - log it into console and you'll see, because also these queries are not chained up and it will run at once thus index will have some value or the last one because it is fast

However , the design of your method is questionable :

Database on parse is a bit different from ordinary sql database , and you should perhaps redesign your method perhaps using one class and just marking object's class by name or changing their state (deployed = 0 / 1 ), it is hard to guess what are you trying to accomplish, but your way guarantees some problems.

Also note that cloudcode functions do have some timeout so once you have more objects you may not be able to save all of them. (150 objects is optimistic view)

You may achieve your solution by writing proper ".beforeSave" function which is triggered when you save your object, this way you can replace old object with new object based on your criteria...

Upvotes: 1

Related Questions