2

I have strings that I have to insert in a db but I want to first modify their value if they fall under certain conditions.

For example I have the strings Epatite, Epatite B, EpatiteáB, EpB3 that I want them to be changed to EP B before being inserted into the db.

This is piece of my code:

// vaccines[index] is the string to compare
var vac = makeUniform(vaccines[index]);
const queryInsert = {
    text: 'INSERT INTO coverages (vaccine) VALUES ($1) ON CONFLICT DO NOTHING;',
    values: [vac]
}
var printText = '[INSERT Italy IN coverages]';
promises.push(postgreSQLlib.query(queryInsert, printText, false));

function makeUniform(val) {
    if(val === 'DIF' || val === 'Difterite') {
        return 'DIPH'; // diphtheria
    }
    else if(val === 'Epatite' || val === 'Epatite B' || val === 'EpatiteáB' || val === 'EpB3') {
        return 'EP B'; // hepatitis B
    }
    else if(val === 'HIB' || val === 'Hib3' || val === 'Hib') {
        return 'HIB'; // haemophilus influenzae B
    }
    else {
        return val;
    }
}

Whene I execute SELECT DISTINCT vaccine FROM coverages ORDER BY vaccine; on psql shell, I get:

DIPH
DT-DTP3
DTP3
EP A
EP B
EpatiteáB
Hib
HIB
M-MPR1
M-MPR1-MPRV ...

There is EpatiteáB which theoretically should have changed in EP B. Why it doesn't work?


EDIT 1

vaccines[index] comes from an online pdf of which I did web scraping using the textract package of Node.js.

Thanks

2 Answers2

0

Try to clean your development database first with this:

UPDATE coverages set vaccine = 'EP B' WHERE vaccine LIKE 'Epatite%' OR vaccine = 'EpB3';

Do something similar for the others.

giubueno
  • 58
  • 8
0

Try this added one more condition = (val==="Epatite%E1B%21")

function makeUniform(val) {
    if(val === 'DIF' || val === 'Difterite') {
        return 'DIPH'; // diphtheria
    }
    else if(val === 'Epatite' || val === 'Epatite B' || val==="Epatite%E1B%21" || val === 'EpatiteáB' || val === 'EpB3') {
        return 'EP B'; // hepatitis B
    }
    else if(val === 'HIB' || val === 'Hib3' || val === 'Hib') {
        return 'HIB'; // haemophilus influenzae B
    }
    else {
        return val;
    }
}
Bhanu Pratap
  • 1,635
  • 17
  • 17