Problems with editable-text and beforeUpdate hook


#1

Hey, folks. I’m working on my first serious Meteor project, and I’m running into a bit of an issue.

I’ve got a small page here that contains a table of Categories, and I am using editable-text to allow for edit-in-place functionality for a couple of the fields. Editing seems to work, but to handle some verification, I’ve written/attached a callback to handle the beforeUpdate callback on the limit field for my document.

The callback seems to be…well…called back, but it’s not having the effect on my data that I believe it should have. Basically, if a user types in a value of something like 43, I want this callback to force the value to 43.00 (two decimal places mandatory).

This code segment contains the callback itself:

EditableText.registerCallbacks({

    // Callback for editing category limit/budget.
    budgetCurrency: function(doc) {
        // Variables based on editing.
        var oldVal = this.oldValue;
        var newVal = this.newValue;

        // Verification:
        if (isNaN(newVal)) {
            // Reject value if not a number, don't make change.
            oldVal;
        } else {
            newVal = Number(newVal);
            console.log(newVal.toFixed(2));
            return _.extend(doc, {limit: newVal.toFixed(2)});
        }
    }
});

I’m not sure if I’m doing something wrong or not. The value that is returned via console.log() is just as I would expect it to be, however the same value (in code) is not what ends up being stored in the updated document.

Any assistance would be greatly appreciated. I could just be overlooking something as far as how beforeUpdate works (callback), for all I know.

Documentation for the package in question is located at: https://atmospherejs.com/babrahams/editable-text


#2

I assume you’ve not done (from the repo readme):

type="number" will mean the value entered is stored as a NumberInt value in mongo (the default is type="string")

You could check the data in the doc before it gets back to the calling function:

replace

return _.extend(doc, {limit: newVal.toFixed(2)});

with

doc.limit = newVal.toFixed(2);
console.log(doc);
return doc;

As you’ll be aware, toFixed() makes a string, so that’s what you should see in doc.limit.


#3

Sorry, just saw this now (over a month after the fact). I think this is an issue with the babrahams:editable-text package. If the type is “number”, the number being saved to the db is run through parseInt, so those two decimal places will be lost. I’ve just opened an issue on github – this is something that needs addressing.