0

I am running parse server in NodeJS environment with express.

Generally, Parse automatically figures out which data has changed so only “dirty” fields will be sent to the Parse Cloud. So, I don’t need to worry about squashing data that I didn’t intend to update.

But why this following code is saving new data every time instead of updating the existing document data with name "Some Name".

// Parse code

Parse.initialize(keys.parseAppID);
Parse.serverURL = keys.parseServerURL;

var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();

let data = {
    playerName: "Some Name",
    score: 2918,
    cheatMode: true
};



gameScore.save(data, {
            success: (gameScore) => {
                // let q = new Parse.Query("GameScore");
                // q.get(gameScore.id)
                console.log("ID: " + gameScore.id)

            },
            error: function (gameScore, error) {
                // Execute any logic that should take place if the save fails.
                // error is a Parse.Error with an error code and message.
                alert('Failed to create new object, with error code: ' + error.message);
            }
        });




// End of Parse code
1
  • Hey @perception30, if my answer worked for you could you mark it? Thanks! Commented Aug 22, 2017 at 14:17

1 Answer 1

1

The problem is that you're executing the query to find which object you want to update, but then you're not using the results when you go to save data.

query.first({ // This will result in just one object returned
    success: (result) => { 
        // Check to make sure a result exists
        if (result) {
            result.save(data, {
                // Rest of code

Note: You're treating playerName as a unique key. If multiple users can have the same playerName attribute, then there will be bugs. You can use id instead which is guaranteed to be unique. If you use id instead, you can utilize Parse.Query.get

Edit: Since you want to update an existing object, you must specify its id.

var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();
gameScore.id = "ID"; // This id should be the id of the object you want to update
Sign up to request clarification or add additional context in comments.

11 Comments

I just updated the code. please review. This should update the existing object instead of creating a new object every time I run the code. But It is creating a new duplicate object every time instead of updating existing one.
using a unique key like id/email does not solve the issue as well.
What existing object? You're not fetching any object from the database.
My Parse server (mongodb database) has an existing document object with exactly same data.
It doesn't have the same id though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.