0
votes

I'm struggling on an algorithmn to delegate votes in a liquid democracy system. My issue here is that Mongoose has a quite unexpected behavior. While using .findOneAndUpdate, it returns me an updated document (in my case, with weight=0 as I want), but if I look in the database afterward, the document was not updated.

Here is the code: Vote.count({propId: propId, delegation: true}, function(err, voteNumber) {

var maxDelegation = voteNumber;

for(var d=1; d<maxDelegation+1; d++){

        Vote.find({propId: propId, delegation: true, weight: d}, function(err, specWeightVotes){

            for (var i = 0; i < specWeightVotes.length; i++) {

                console.log(specWeightVotes[i].content);

                // Add the weight d, aka the weight of a vote to the delegated voter
                Vote.findOneAndUpdate({propId: propId, voter: specWeightVotes[i].content }, { $inc: { 'weight': specWeightVotes[i].weight }}, function (err) {
                    if (err) {
                        console.log("Ca bug.");
                    }
                });

                //Put weight as zero for the ones who have delegated
                Vote.findOneAndUpdate({_id: specWeightVotes[i]._id}, { $set: { 'weight': 0 }}, {new: true}, function (err, vote) {
                        if (err) {
                            console.log("Ca bug 2.");
                        }
                        console.log(vote);
                });
            }
        });
    }
    console.log("I have delegated all votes");
});

In the second .findOneAndUpdate, the results of the console.log are:

{
_id: '5a587a96445e1900474d6ae3',
propId: '5a587a84445e1900474d6ade',
voter: 'someVoterID',
delegation: true,
content: 'someOtherVoterID',
weight: 0,
__v: 0 }

Which is the expected result. However, in the database, looking after the function executes, the weight remains at 1, or X in case it's higher:

{
_id: '5a587a96445e1900474d6ae3',
propId: '5a587a84445e1900474d6ade',
voter: 'someVoterID',
delegation: true,
content: 'someOtherVoterID',
weight: 1,
__v: 0 }

As a extra info, no delegation is coming to add up a weight on the given document.

1

1 Answers

0
votes

try follow code and let me know the result.

 var maxDelegation = voteNumber;

 for(var d=1; d<maxDelegation+1; d++){

   let specWeightVotes = Vote.find({propId: propId, delegation: true, 
   weight: d}).map(function(k){
    return k;
   });

   for (var i = 0; i < specWeightVotes.length; i++) {

    console.log(specWeightVotes[i].content);

    // Add the weight d, aka the weight of a vote to the delegated voter
    Vote.findOneAndUpdate({propId: propId, voter: 
    specWeightVotes[i].content }, { $inc: { 'weight': 
    specWeightVotes[i].weight }}, function (err) {
       if (err) {
          console.log("Ca bug.");
       }
    });

    //Put weight as zero for the ones who have delegated
    Vote.findOneAndUpdate({_id: specWeightVotes[i]._id}, { $set: { 
    'weight': 0 }}, {new: true}, function (err, vote) {
       if (err) {
          console.log("Ca bug 2.");
    }
          console.log(vote);
    });
   }
  }
 console.log("I have delegated all votes");
});