mongoose-currency icon indicating copy to clipboard operation
mongoose-currency copied to clipboard

$inc not calling method

Open petrogad opened this issue 11 years ago • 4 comments

When executing an update on a record with the $inc call, it doesn't look like the Currency prototype is ever called.

Is there a way to catch these too so that processing could happen on inc'd values?

Thank you!

petrogad avatar Feb 06 '14 13:02 petrogad

@petrogad Could you send a snippet of the Mongoose schema you have and how you are using inc?

I'm not sure there will be an easy way to fix this, but I'll take a look!

paulcsmith avatar Feb 11 '14 15:02 paulcsmith

You bet!

Schema:

MemberSchema = new Schema({
    memberName: String,
    memberDeposit: {type: Currency}, // doing an $inc on this
    memberWithdrawal: {type: Currency}, // doing an $inc on this
    memberBalance: {type: Currency}, // doing an $inc on this which is either + or -
    transactions:[{type: ObjectId, ref:"Transactions"}],
    _groupId: {type: ObjectId, ref:"Group"}
});

Also I noticed that if I added values b and c to the Currency prototype, C is used to denote a fetch from the DB; and I could divide by 100 here. I haven't been able to find any documentation on what the parameters are though, so I'm not 100% confident in it.

You can see my forked version for full value, however If c stands true i'd likely refactor parts of it for better reuse.

Thanks!!

Currency.prototype.cast = function (val, b, c) {
    if(c==true){
        //Apparently this is when it's being pulled out of the table
        return val/100;
    }
  ...
}

petrogad avatar Feb 11 '14 15:02 petrogad

I should also say that my initial expectation was that all values marked as currency were multiplied out by 100 (not just numbers); and was hoping that when doing an $inc of something like 10.32 it would actually $inc 1032 through the library.

Thanks again!

petrogad avatar Feb 11 '14 15:02 petrogad

I'm no longer using node so I won't ever get to this, but if someone wants to take a stab at a PR I'll merge it in

paulcsmith avatar May 15 '17 15:05 paulcsmith