sql >> Database >  >> RDS >> PostgreSQL

INSERT-bewerking met Node.js en PostgreSQL werkt niet

Er zijn twee fouten in het app.js-bestand.

const postgreSQLlib = require('./middlewares/postgreSQLlib.js')
const downloaderCoverageIta = require('./routers/scraper.js');

const start = async function() {
    // connect
    await postgreSQLlib.connect();

    // create tables
    var queryCreateCoverages = {
        text: 'CREATE TABLE IF NOT EXISTS coverages ('+
                    'vaccine VARCHAR(64),' + 
                    'country VARCHAR(255),' +
                    'region VARCHAR(255),' +
                    'year VARCHAR(4),' +
                    'value VARCHAR(12),' +
                    'PRIMARY KEY(vaccine, country, region, year))'
    };
    var queryRes = await postgreSQLlib.query(queryCreateCoverages);
    //console.log('Result: ', queryRes);

    // get data
    await downloaderCoverageIta.download();

    const query = {
        text: 'SELECT * FROM coverages WHERE country = $1',
        values: ['Italy']
    }
    return await postgreSQLlib.query(query);
    //console.log('Result: ', queryRes);

    // disconnect 
    await postgreSQLlib.disconnect();
}

// start application
start()
.then(function(res) {
    console.log(res);
})
.catch(function(err) {
    console.log(err);
});

Eerst gebruikte je de juiste var "downloaderCoverageIta" niet, daarna gaf je de waarde niet terug.

En weinig in srcapper.js bestand

var cheerio = require('cheerio');
var request = require('request-promise');
var util = require('../helpers/util.js');
var postgreSQLlib = require('./postgreSQLlib.js');

var methods = {};

var countries = {
'Italy': 'ITA',
'Latvia': 'LVA',
'Netherlands': 'NLD'
};

var outDir = './output/';
var finalData = outDir + 'coverage-eu.json'

var jsons = [];

methods.download = async function(req, res) {
await Promise.all(Object.values(countries).map(async country=> {
    var url = 
'http://apps.who.int/immunization_monitoring/globalsummary/coverages?c=' + country;

    const html = await request(url);
    $ = cheerio.load(html);

    var years = [];
    var vaccines = [];
    var coverages = [];

    $('.ts .year').each(function() {
        years.push($(this).text().trim());
    });
    $('.ts .odd td a, .ts .even td a').each(function() {
        vaccines.push($(this).text().trim());
    });
    $('.ts .odd .statistics_small, .ts .even 
.statistics_small').each(function() {
        coverages.push($(this).text().trim());
    });

    const numYears = years.length;
    const numVaccines = vaccines.length;
    for(var vaccineIdx = 0; vaccineIdx < numVaccines; vaccineIdx++) {
        for(var yearIdx = 0; yearIdx < numYears; yearIdx++) {
            let obj = {
                year: years[yearIdx],
                country: country,
                region: "",
                vaccine: vaccines[vaccineIdx],
                coverage: coverages[vaccineIdx*numYears + yearIdx]
            }

            // save on db
            const query = {
                text: 'INSERT INTO coverages (vaccine, country, 
region, year, value) VALUES($1, $2, $3, $4, $5)',
                values: [vaccines[vaccineIdx], country, '', 
years[yearIdx], coverages[vaccineIdx*numYears + yearIdx]],
            }
            var queryRes = await postgreSQLlib.query(query);

            // save on file
            jsons.push(obj);
        }
    }
    await util.printOnFile(jsons, finalData);
}))
}

module.exports = methods;

Zoals ik weet, werkt de aanvraagmodule niet met de asynchrone wacht. Dit werkte in ieder geval voor mij.

Dit is van mijn pg-beheerder nadat ik de code heb uitgevoerd.




  1. Postgres:beperking toevoegen als deze nog niet bestaat

  2. Android haalt gegevens op uit de MySql-tabel

  3. mysql invoegen in 2 tabellen tegelijk die een primaire sleutel en een externe sleutel hebben

  4. Dynamische WHERE-component bouwen in opgeslagen procedure