0

I believe I am misunderstanding how the Parse JS SDK when it comes to handling subscriptions via Parse LiveQuery. In my Cloud Code folder (hosted on a server), I am able to invoke the following function from my front end client:

getMatch: async function(req, res){
    try {
      const query = new Parse.Query('match');
      const match = await query.get(req.params.id);
      const subscription = query.subscribe();
       
      subscription.on('open', function() {
        console.log('Subscription Opened');
        let matchObject = mapMatch(match);
        res.success(matchObject);
      });
  
      subscription.on('update', function(response) {
        console.log('Calling Update');
        let updatedMatch = response;
        let matchObject = mapMatch(updatedMatch);
        console.log(matchObject);
        
        res.success(matchObject); <- Unable to fire
        
      });
  
    } catch (error) {
      console.log(error.message);
    }
},

From what I am understanding from what I'm observing, 'res.success(matchObject)' will be invoked successfully within the 'on' subscription block where my front-end will receive and consume the returned data successfully.

When I then continue to update my data from the front end, it will successfully update the record and the 'update' subscription block seen above will be called successfully also (I can see both console logs with the correct data).

However, no new data is received by the front-end and therefore no changes are visible until I refresh the browser, which isn't quite the effect I'm looking for. From this I can assume that 'res.success(matchObject)' is not being fired in the 'update' subscription block.

I'm now assuming that 'res.success' cannot be invoked more than once the way I am currently implementing the functionality, and therefore wont fire again once I enter the 'update' subscription block. If that is the case, is there an alternative way of implementing this? Or have I approached the solution totally incorrectly?

For context , the flow of my code is as follows:

functions.js (Cloud Code)

Parse.Cloud.define('get_match', async function(req,res) {
  await matches.getMatch(req,res);
});

api-service.ts (Front-End Client)

public async getMatch(req): Promise<any>{
    let response = await Parse.Cloud.run("get_match",req);
    return response;
}

component.ts (Front-End Client)

private assignMatch(){

    this.apiService.getMatch({id : this.matchId}).then( matchResponse => {
        console.log(matchResponse)
        // ... 
    )};

}

I also understand that using Promises (which is how I'm currently implementing this solution) don't return observables and therefore wont be able to be 'subscribed' to in the front end, which is what I've been used to. Perhaps there is a way to do this in Parse SDK? The Parse SDK docs are definitely useful : https://docs.parseplatform.org/js/guide/#live-queries , however I may have approached this incorrectly from my end.

Joe Iacono
  • 43
  • 1
  • 7

0 Answers0