1
votes

I need to modify / add an attribute to a Parse object before returning it, but not save it to the database.

Here's my scenario: I do a query for a single record and when successful, I need to add a field to that object:

... query.first({

  success: function(result) {



  // get current and expiration date and calculate expiration

  var now = new Date();

  var expiresInHours = moment(results.get("expiresAt")).diff(now, 'hours');



  // set attribute                        

  result.add("expiration", expiresInHours);       



  // respond to client

  response.success(result);                    

The problem is that I always get an error, because I modified an object and didn't save it.

I would really like to return just the object instead of having to create a new object like

var answer = {"data": result, "expiration": expiresInHours};

which would work, but then I would need to change a lot of code on the client side....

Any help? Thx, Martin.

1

1 Answers

0
votes

On using the "set" method for the object, it will try to save the object where that particular column will not be there so try converting the result to a JSON and try inserting attr to the objects.

var resultsJson = [];
for (var i = 0; i<results.length; i++) {
 var resultJson = (results[i].toJSON());
  if (relations[results[i].get("user").id] != null) {
   resultJson["relationship"] = someValue;
  }
 resultsJson.push(resultJson);
}
response.success(resultsJson);`