PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Operasi INSERT menggunakan Node.js dan PostgreSQL tidak berfungsi

Ada dua kesalahan dalam file 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);
});

Pertama, Anda tidak menggunakan var "downloaderCoverageIta" yang benar, lalu Anda tidak mengembalikan nilainya.

Dan beberapa di file 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;

Sesuai yang saya tahu modul permintaan tidak berfungsi dengan async menunggu. Ini setidaknya berhasil untuk saya.

Ini dari pg admin saya setelah saya menjalankan kodenya.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQLAlchemy atau psycopg2?

  2. Tabel dalam memori di PostgreSQL

  3. Status HTTP 500 - java.lang.NoClassDefFoundError:java/time/temporal/TemporalField saat menjalankan aplikasi di OpenShift

  4. to_sql + sqlalchemy + salin dari + mesin postgresql?

  5. date_trunc PostgreSQL di mySQL