Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not update my collection from parse cloud?

I am facing an issue in parse cloud code. The following is updating score and change date in my gamescore table. But it is not working. while I am doing same in my web code and it is working fine. Am I doing anything wrong here ?

'use strict';
var GameScore = Parse.Object.extend('GameScore');
Parse.Cloud.define('editScore', function(req, res) {
  var query = new Parse.Query(GameScore);
  query.get(req.params.objectId, {
    success: function(gameScore) {
      gameScore.set('score', req.params.score);
      gameScore.set('date', req.params.date);
      gameScore.save(null);
      gameScore.fetch(myCallback);
    },
    error: function(err) {
      return res.error(err);
    }
  });
});

If so then please help me so that I can make it working.

like image 212
Kundu Avatar asked Aug 12 '14 10:08

Kundu


3 Answers

Try adding Parse.Cloud.useMasterKey(); inside the function to bypass any ACL restrictions that could be causing an issue. Example:

var GameScore = Parse.Object.extend('GameScore');
Parse.Cloud.define('editScore', function(req, res) {

  // use Master Key to bypass ACL
  Parse.Cloud.useMasterKey();

  var query = new Parse.Query(GameScore);
  query.get(req.params.objectId, {
    success: function(gameScore) {
      gameScore.set('score', req.params.score);
      gameScore.set('date', req.params.date);
      gameScore.save(null);
      gameScore.fetch(myCallback);
    },
    error: function(err) {
      return res.error(err);
    }
  });
});
like image 139
Niraj Shah Avatar answered Nov 14 '22 04:11

Niraj Shah


You have 3 issues:

  • you're not waiting for the save to finish
  • you're not calling res.success()
  • you're referencing myCallback which from what you've shown us isn't defined

Simple solution is to replace this line:

gameScore.save(null);

With this code:

gameScore.save().then(function () {
    res.success();
});

If you really do need that fetch call you would chain that in:

gameScore.save().then(function () {
    return gameScore.fetch(myCallback);
}).then(function () {
    res.success();
});
like image 31
Timothy Walters Avatar answered Nov 14 '22 03:11

Timothy Walters


var GameScore = Parse.Object.extend('GameScore');
Parse.Cloud.define('editScore', function(req, res) {
  Parse.Cloud.useMasterKey();
      var query = new Parse.Query(GameScore);
      query.get(req.params.objectId, {
        success: function(gameScore) {
          gameScore.set('score', req.params.score);
          gameScore.set('date', req.params.date);
          gameScore.save().then(function() {
                  gameScore.fetch(callback);
                });
        },
        error: function(err) {
          return res.error(err);
        }
      });
    });

using master key we are overriding acl. using then promise method we are calling callback functions after otherwise there is a possibility to get the old data.

like image 24
Kundu Avatar answered Nov 14 '22 04:11

Kundu