PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

Der INSERT-Vorgang mit Node.js und PostgreSQL funktioniert nicht

Es gibt zwei Fehler in der Datei app.js.

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);
});

Zuerst haben Sie nicht die richtige Var "downloaderCoverageIta" verwendet, dann haben Sie den Wert nicht zurückgegeben.

Und wenige in der Datei srcapper.js

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;

Wie ich weiß, funktioniert das Anforderungsmodul nicht mit dem asynchronen Erwarten. Das hat zumindest bei mir funktioniert.

Dies ist von meinem Pg-Administrator, nachdem ich den Code ausgeführt habe.