Return Plain Objects from Cloud Code

I think query.toJSON serialises the query itself and not the results of the query. The result is basically an array of Parse.Objects if json:true is not passed or an array of plain objects if it is.

The problem is that the map method only calls the toJSON function on the objects in the array and it does not recursively call it on the nested objects. I’d probably need to manually do that, not sure if there is a straight, one-liner JS way of doing it.

With the query json option it seems that a __type: 'Object' field is added to the nested objects and that causes the encoder in Parse.Cloud.run function to convert those nested objects into Parse.Objects. So this method would work well if you’d have the option to fetch the results directly from a query, but it doesn’t work if you “wrap” that query in a cloud function because of the encoder call from Parse.Cloud.run.

A solution like this, where we iterate recursively and just remove the __type field instead of recursively calling toJSON(like we could do with the map solution):

// In Cloud code
function removeType(obj) {
    for (prop in obj) {
        if (prop === '__type')
            delete obj[prop];
        else if (typeof obj[prop] === 'object')
            removeType(obj[prop]);
    }
}
...
const results = await cardsQuery.find({ json: true });
removeType(results);
return results;

Works quite well(and can probably be further optimised), but it’s not pretty and it would be ideal to be able to reach the same result just by using the json option in the Query.find function.

I’m still not sure if the added __type field is a bug or not, since it seems to only appear in the nested objects(eg. included pointers) and does not appear in the top level objects in the results array…