Search code examples
parse-platformparse-serverparse-javascript-sdk

Property unset by CloudCode is overwritten by local Parse cache


I have a Cloud Code function which gets passed the id of an existing Parse object, manipulates and saves that object, and returns it to the client (which is implemented with the JS SDK). This generally works well, with one exception: if the CC function unsets a previously set property, the client still sees the old value in the returned object (even though the property is correctly saved as undefined in the db); only when the object is refetched by the client, the property has the correct value.

So here's a simple CC function exposing the issue:

function manipulate(req, res) {
  new Parse.Query('MyObject')
    .get(req.params.id)
    .then(obj => {
      obj.unset('foo');
      return obj.save();
    })
    .then(obj => res.success(obj));
}

And here is the client-side code:

obj.set('foo', 'bar'); // obj was fetched with a query
Parse.Cloud.run('manipulate', { id: obj.id })
  .then(updatedObj => {
    // 'foo' should be undefined but still is set to 'bar'
    assert(updatedObj.get('foo') === 'bar');
  });

Apparently the returned object contains no information about the fact that the property was unset, and so Parse keeps using the cached value of foo.

I consider this to be a bug, but if I made any mistakes in my usage of the API I'd be more than happy to be proven wrong. Any hints on how to solve this are welcome!

Please note: I don't want to use obj.set('foo', null) (which would actually work) because I want to avoid having two different values (null and undefined) representing the 'not set' state.


Solution

  • I ended up using an ugly workaround: Whenever I call a function which might unset a property I call fetch on the returned object(s) to make sure I'm not seeing cached values. A terrible solution performance-wise because I have a second client-server roundtrip.

    If you know a better solution, please add an answer or a comment.