I am working on an app which (amongst other things) allows CSV upload, converts that into JSON and then imports the JSON into mongo.
I have a schema which looks like:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var statementSchema = new Schema({
date : {type:Date, required:true},
name: {type:String, required:true},
method: {type:String},
amount: {type:Number, required:true},
category: {type:String, default:'Not Set'},
importDate: {type : Date, default:Date.now, required:true}
});
statementSchema.index({"date":1, "name":1, "amount":1}, {unique: true});
module.exports = mongoose.model('Statement', statementSchema);
I have an import script which loops through a JSON object and persists according to the schema:
for( var i = 0; i < statements.length; i++ ) {
.....
var newStatement = new Statement();
//the amount value is a string currently
newStatement.amount = Number(statements[i].amount);
...
}
However as I say any objects that have an amount that includes a comma e.g. 1,120.55
do not get persisted.
I have tried mongoose-currency which resolves the issue but then I end up with a value such as 112055. The docs say to use .toFixed(2)
when showing to users but I am running a .find()
query in my route which is then passed to Handlebars to render.
I've tried various handlebars functions
Handlebars.registerHelper('amountFixed', function(amount) {
return amount.toFixed(2);
});
Handlebars.registerHelper('amountCurrency', function(amount) {
return amount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
});
(as well as format.js and handlebars.numeral) to try to correct the formatting but I can't quite get it right.
So my question is - what is the best way of importing a value with a comma into mongo and then showing it to a user?
via Stuart Brown
No comments:
Post a Comment