Mr. R
Mr. R

Reputation: 11

Regex doesn't work. What's wrong with my code? Can anyone help me fix this

My code:

property_unit_plan.post('/bulkAdd',(req, res) =>{
    Array.prototype.forEach.call(req.body, element => {
        db.sequelize.query('CALL sp_property_unit_plan_add_bulk( :unit_size_range, :no_of_bedrooms, :no_of_balcony, :no_of_washrooms, :unit_type, :is_hall, :unit_price, :bill_period)', {
            replacements: {
                unit_size_range: (element.unit_size_range).replace(/[,]+/g, ""),
                no_of_bedrooms: element.no_of_bedrooms,
                no_of_balcony: element.no_of_balcony,
                no_of_washrooms: element.no_of_washrooms,
                unit_type: element.unit_type,
                is_hall: element.is_hall,
                unit_price: (element.unit_price).replace(/[,]+/g, ""), 
                bill_period: element.bill_period
            }
        }).then(res => {
            res.send({ error: false, message: `Successfuly added.` });
        }).catch(err =>{
            res.send({ error: true, message: `Error 767: ${err}` });
        });
    });
});

Return value, unit_price always returning with commas.

return value after regex

Upvotes: 1

Views: 84

Answers (1)

Daantje
Daantje

Reputation: 2486

Why using a regex? You can convert to a float like this:

parseFloat(element.unit_price.replaceAll(',','')),

Upvotes: 1

Related Questions