Giter Site home page Giter Site logo

mongoose-money's Issues

Bug that prevents 0.0 value from being retrieved from database

Hi,

I found a bug in your code that allows 0.0 amount to be stored in the database, but when you retrieve the value, a casting exception is thrown. This occurs because you have a place in index.js (at line 147) where you test for "value.amount", which in the case of a zero value will be false. I have fixed this by changing the test to "(value.amount !== undefined)", which does fix the problem. Here is the full code:

MongooseMoney.prototype.cast = function(value /,doc , init/ ) {
//TODO make use of base currency to cast value to
//money
// console.log(value);
if (null === value) {
return value;
}

if ('' === value) {
    return null;
}

if (value instanceof Money) {
    return value;
}

if (value && (value.amount !== undefined) && value.currency) {
    if (value.time && value.time instanceof Date) {
        return new Money(
            value.amount,
            Money[value.currency],
            value.time
        );
    } else if (value.time && value.time instanceof String) {
        return new Money(
            value.amount,
            Money[value.currency],
            new Date(value.time)
        );
    } else {
        return new Money(value.amount, Money[value.currency]);
    }
}

throw new SchemaType.CastError('MongooseMoney', value);

};

I am using this in a new project, and was wondering if you could please put this change in soon so I can get it in "official" form? Hopefully this will make it super easy for you to do so :)

Cheers,
Ken

Database representation uses Double

When storing values on mongo, the money amount is stored as a Double value, losing precission, which is not admissible for money.

See https://docs.mongodb.com/v3.2/tutorial/model-monetary-data/ for ideas on how to store money in mongodb.

This is extra infuriating because moneyjs, the library this one uses to model Money internally, uses big.js to model money amounts, which correctly stores arbitrary precission, but at some point that is converted to a regular JS Number and finally stored as a Double in mongo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.