简体   繁体   中英

Javascript Parse SDK saving twice

I am a little bit confused about this code in the parse javascript SDK.

// Create the object.
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();

gameScore.set("score", 1337);
gameScore.set("playerName", "Sean Plott");
gameScore.set("cheatMode", false);
gameScore.set("skills", ["pwnage", "flying"]);

gameScore.save(null, {
  success: function(gameScore) {
    // Now let's update it with some new data. In this case, only cheatMode and score
    // will get sent to the cloud. playerName hasn't changed.
    gameScore.set("cheatMode", true);
    gameScore.set("score", 1338);
    gameScore.save();
  }
});

Can someone explain why it is saving twice? https://parse.com/docs/js_guide#objects-updating

[EDIT] To be clear I understand there are two calls to .save();

I want to know why this code, which is straight from the parse Javascript SDK, is saving twice. Are there benefits or is this a mistake? I am not sure..

The reason it is saving twice is because on a successful save of gameScore you are calling save again.

gameScore.save(null, {    //FIRST SAVE
    success: function(gameScore) {
        gameScore.set("cheatMode", true);
        gameScore.set("score", 1338);
        gameScore.save(); //SECOND SAVE
    }
});

On a successful save, you typically shouldn't be saving it again.

In the link you gave, they're just showing you that you can update gameScore with new data. It's not, generally, a practical use of the save() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM