使用Node.js和PostgreSQL的INSERT操作不起作用

时间:2018-03-28 15:28:38

标签: sql node.js postgresql async-await

我正在使用Node.js和PostgreSQL创建我的第一个应用程序。 此应用程序连接到db,创建表,对Web站点进行Web抓取,将信息插入db,然后从db断开连接。 我尝试使用async/await

问题是插入操作不起作用。没有错误,只需将表 coverage 保持为空。

这是我的代码。

app.js

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

const start = async function() {
    await console.log('START');

    // connect
    await postgreSQLlib.connect();

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

    // get data
    await scraper.download();

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

    // disconnect 
    await postgreSQLlib.disconnect();

    return 'FINISH';
}

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

postgreSQLlib.js

var fs = require('fs');
const {Client} = require('pg'); // node-postgres npm package

const userDb = 'admin';
const passDb = 'admin';
const hostDb = 'localhost'; // default
const portDb = '5432'; // default
const nameDb = 'db'; 
const connectionString = 'postgres://' + userDb + ':' + passDb + '@' + hostDb + ':' + portDb + '/' + nameDb; // result is 'postgres://admin:admin@localhost:5432/db';
let client;

var methods = {};

methods.connect = async function() {
    client = new Client({connectionString});
    return await client.connect()
    .then(async function() {
        await console.log('\nConnected to ' + client.database + ' at ' + client.host + ':' + client.port + ' as ' + client.user + ' (pass: ' + client.password + ')');
    })
    .catch(function(err) {
        console.log('\nError during connection to PostgreSQL');
        throw err;
    });
}

methods.query = function(query) {const start = Date.now();
    return client.query(query)
    .then(function(res) {
        const duration = Date.now() - start;
        console.log('\nExecuted query: {\n  ' + query.text + '\n  [' + query.values + ']' + '\n  duration: '+ duration + '\n  rows: ' + res.rows.length + '\n}');
        return res;
    })
    .catch(function(err) { 
        console.log('\nError executing query', err.stack);
    });
}

methods.disconnect = async function() {
    await client.end()
    .then(function() {
        console.log('\nConnection has ended');
    })
    .catch(function(err) {
        console.log('\nError during clossing connection');
        throw err;
    }); 
}

module.exports = methods;

scraper.js

var cheerio = require('cheerio');
var request = require('request');
var util = require('../helpers/util.js');
var postgreSQLlib = require('../middlewares/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) {
    for(country in countries) {
        var url = 'http://apps.who.int/immunization_monitoring/globalsummary/coverages?c=' + countries[country];

        return await request(url, (async function(country) {
            var thisCountry = country;

            return async function(error, res, html) {
                if(error) {
                    throw error;
                }

                $ = 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++) {
                        // 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);
                        //console.log('Result: ', queryRes);
                    } 
                }
            } 
        })(country));
    } 
};

module.exports = methods;

为什么呢?如果没有显示错误,我无法解决错误。

对于我糟糕的英语,任何帮助都表示赞赏和抱歉。

编辑1

如果我使用SQL shell(psql)插入记录,它可以工作:

enter image description here

编辑2

我修改了console.log方法的query

methods.query = async function(query, print) {
    return await client.query(query)
    .then(function(res) {
        console.log(print, 'OK query');
        return res;
    })
    .catch(function(err) { 
        console.log(print, 'ERR query');
    });
}

scraper.js

methods.download = async function(req, res) {
    for(country in countries) {
        console.log('\nCOUNTRY:', country); // ADDED
        var url = 'http://apps.who.int/immunization_monitoring/globalsummary/coverages?c=' + countries[country];
        let res = await request(url);
        insert(res);
    }
}

//...

// save on db
const queryInsert = {
    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 printText = '[INSERT ' + country + ' IN coverages]';
var queryRes = await postgreSQLlib.query(queryInsert, printText);

这些是版画:

START

Connected to db at localhost:5432 as admin (pass: admin)
[CREATE TABLE coverages] OK query

COUNTRY: Italy

COUNTRY: Latvia
[INSERT Italy IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query

COUNTRY: Netherlands
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query
[INSERT Netherlands IN coverages] OK query

Connection has ended

FINISH
[INSERT Netherlands IN coverages] ERR query

如您所见,最后会出现错误,因为连接已经关闭。 此外,与查询结果相关的打印不是针对国家/地区订购的。例如:

COUNTRY: Italy

COUNTRY: Latvia
[INSERT Italy IN coverages] OK query
[INSERT Latvia IN coverages] OK query
[INSERT Latvia IN coverages] OK query

COUNTRY:Italy 下面没有任何内容,但有关意大利查询的结果低于 COUNTRY:拉脱维亚(并且不是全部)。

1 个答案:

答案 0 :(得分:3)

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

首先你没有使用正确的var“downloaderCoverageIta”然后你没有返回值。

在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;

据我所知,请求模块不能与async await一起使用。这至少对我有用。

enter image description here 这是在我运行代码后来自我的pg admin。