gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:03:37 25 4
gpt4 key购买 nike

我正在使用 Node.js 和 PostgreSQL 创建我的第一个应用程序。这个应用程序连接到数据库,创建表,对网站进行网页抓取,将信息插入数据库,然后断开与数据库的连接。我正在尝试使用 async/await

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

这是我的代码。

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

我修改了query方法的console.log:

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: Latvia(并非全部)。

最佳答案

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;

据我所知,请求模块不适用于异步等待。这至少对我有用。

enter image description here这是我运行代码后来 self 的 pg 管理员的。

关于sql - 使用 Node.js 和 PostgreSQL 的 INSERT 操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com