ParseReact icon indicating copy to clipboard operation
ParseReact copied to clipboard

Calling `toPlainObject` on an array

Open taylorstine opened this issue 10 years ago • 1 comments

What is the purpose of this function? https://github.com/ParsePlatform/ParseReact/blob/master/src/flatten.js#L29-L37 specifically, why does calling toPlainObject on an array of Parse.Object's return a different value than calling it on a single object? For instance, I wanted to do this:


function _deleteEntries (entries) {
/**Just for reference, nothing exciting here**/
ParseReact.Mutation.Destroy(entry)
        .dispatch()
        .then(() => {
          return new Parse.Query('Entry')
            .containedIn('objectId', _.pluck(entry.entries, 'objectId'))
            .find()
        })
        .then((subEntries)=>{
           /** this is where it's interesting **/
          subEntries = Parse.Object.prototype.toPlainObject.call(subEntries)
          this._deleteEntries(subEntries);
        })
}

where an Entry can have a list of Entries which point to sub lists etc. But I could not access the second depth of Entries because calling toPlainObject on an array returns an object without the attributes, instead it returns an object with just className and objectId So instead I had to do something like this:


function _deleteEntries (entries) {
/**Just for reference, nothing exciting here**/
ParseReact.Mutation.Destroy(entry)
        .dispatch()
        .then(() => {
          return new Parse.Query('Entry')
            .containedIn('objectId', _.pluck(entry.entries, 'objectId'))
            .find()
        })
        .then((subEntries)=>{
           /** this is where it's interesting **/
          subEntries = _.map(subEntries, function(subEntry){
            var obj = subEntry.toPlainObject();
            return obj;
          });
          this._deleteEntries(subEntries);
        })
}

taylorstine avatar Sep 12 '15 18:09 taylorstine

toPlainObject is a utility function for converting the JS SDK's current representation of a Parse Object into a flattened version ideal for Parse+React mutations. This is typically used for the results coming back from a cloud function, but your use case is also valid.

The specific reason we convert arrays of objects into arrays of pointers is that only the latter behaves properly in the Parse API / backend. When you use the SDK directly and save an array of Parse Objects, they will automatically be converted to pointers before they hit our REST API.

In your case, the conversion to a plain object shouldn't be necessary -- Mutations automatically handle receiving SDK-formatted Parse.Objects. Assuming your code takes in an array of entries and loops over them, you could just rewrite without any toPlainObject calls:

function _deleteEntries(entries) {
  entries.forEach((entry) => {
    ParseReact.Mutation.Destroy(entry)
      .dispatch()
      .then(() => {
        // ...
      })
      .then((subEntries) => {
        this._deleteEntries(subEntries)
     })
    // ... etc.

andrewimm avatar Sep 14 '15 19:09 andrewimm