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.
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);
}
});
});
You have 3 issues:
res.success()
myCallback
which from what you've shown us isn't definedSimple 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();
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With