can not update my collection from parse cloud?
Asked Answered
R

3

9

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.

Rhynchocephalian answered 12/8, 2014 at 10:8 Comment(0)
C
1

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);
    }
  });
});
Cygnus answered 12/8, 2014 at 12:12 Comment(0)
C
1

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();
});
Calchas answered 13/8, 2014 at 8:54 Comment(0)
R
1
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.

Rhynchocephalian answered 13/8, 2014 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.