Skip to content

I can't prevent creating duplicated documents. #4307

Closed
@okanakdogan

Description

@okanakdogan

Issue Description

I have similiar issue with this stackoverflow question.

I want to create objects to a collection for per user. I will save scores in this collection. When I want to update user's score, if there isn't a score entry in that collection, i want to create a new object. if there is a score object i want to update it.

But with my current code, sometimes creates new score entries even there is a entry for that user.

function saveScore(user_id , score){
  var query= new Parse.Query(ScoreObject);
  query.equalTo("user_id",user_id );

  query.first().then(
        function(result){
               if(result === undefined){
                     // there isn't entry for this id
                     //create ScoreObject and fill it
                     ...
                      newObject.set("user_id",id);
                      newObject.set("score",score);
                      newObject.save();
               }else{
                       // entry found for this id
                       //do update on result and save
                       ...
                       result.increment("score",score);
                       result.save();
                }
         }
   );

}

I'm looping on every played games and calculating points, looking for user and add score that user.

for(int i=0; i<games.length; ++i){
          //calculate point
          //get user id
          var user_id = games[i].get("user_id");
          saveScore(user_id,point); // this should find user's uniqe score entry
          //but creates new score entries eveb if there entry for that user.
}

Steps to reproduce

i have users in User table.
i have a collection that holds scores with user id reference (fields: _id, user_id, month,score)

Expected Results

Score entries for per user.

Actual Outcome

more than one entry for some users.

Environment Setup

  • Server

    • parse-server version (Be specific! Don't say 'latest'.) : [2.6.5]
    • Operating System: Heroku Dyno
    • Hardware: Heroku Web App
    • Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): [Heroku]
  • Database

    • MongoDB version: [3.4.9]
    • Localhost or remote server? (AWS, mLab, ObjectRocket, Digital Ocean, etc): [mLab]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions