I started to learn NodeJS and PostgreSQL and I have some problems with them. I made three tables which called authors, articles and branches. Everything is work good. For example when I wrote this: select * from authors;
it shows me everything, I mean all authors. Then I decided to make server using NodeJS. And when it laucnhed I will write in browser this for example http://localhost/articles
and it shows me authors and his articles. But I have one error and I don't know how to fix it. Please help me.
This is my code:
const http = require('http');
const path = require('path');
const async = require('async');
const socketio = require('socket.io');
const express = require('express');
var pg = require('pg')
var format = require('pg-format')
var PGUSER = 'person'
var PGDATABASE = 'website'
var config = {
user: PGUSER, // name of the user account
password: 'person',
database: PGDATABASE, // name of the database
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed
}
var pool = new pg.Pool(config)
var myClient;
var data;
var myQuery = `
SELECT
"authors"."name",
"branches"."branch",
"articles"."title",
"articles"."text"
FROM "articles"
INNER JOIN "authors"
USING ("id")
INNER JOIN "branches"
USING ("id")
ORDER BY
"authors"."name";
`;
// var pg = require('pg');
// var conString = "postgres://person:person@localhost:5432/Website";
// var client = new pg.Client(conString);
// client.connect();
pool.connect(function (err, client, done) {
if (err) console.log(err)
myClient = client
var ageQuery = format(myQuery)
myClient.query(ageQuery, function (err, result) {
if (err) {
// return err;
console.log(err);
// return '<h1>err</h1>';
}
console.log(result.rows[0]);
data = result.rows;
// return res.end("<h1>yes</h1>");
})
});
var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
router.use(express.static(path.resolve(__dirname, 'client')));
var parser = require('./controllers/API_parser.js');
server.listen(process.env.PORT || 8080, process.env.IP || "127.0.0.1", function(){
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});
router.get('/articles', function (req, res) {
res.header('Content-type', 'text/html');
var result = "";
for (var i = 0; i < 3; i++) {
result += "<h1>" + data[i]["name"] + "</h1>";
result += "<h3>" + data[i]["branch"] + "</h3>";
result += "<h2>" + data[i]["title"] + "</h2>";
result += "<h4>" + data[i]["text"] + "</h4>";
result += "<hr>";
}
// return res.end(parser.getDataFromDatabase());
return res.end(result);
});
Anf this is my error
Debugger listening on [::]:15454
Server listening at 0.0.0.0:8080
{ error: column "id" specified in USING clause does not exist in left table
at Connection.parseE (/home/ubuntu/workspace/node_modules/pg/lib/connection.js:553:11)
at Connection.parseMessage (/home/ubuntu/workspace/node_modules/pg/lib/connection.js:378:19)
at Socket.<anonymous> (/home/ubuntu/workspace/node_modules/pg/lib/connection.js:119:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
name: 'error',
length: 133,
severity: 'ERROR',
code: '42703',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_clause.c',
line: '876',
routine: 'transformFromClauseItem' }
/home/ubuntu/workspace/server.js:57
console.log(result.rows[0]);
^
TypeError: Cannot read property 'rows' of undefined
at Query.callback (/home/ubuntu/workspace/server.js:57:21)
at Query.handleError (/home/ubuntu/workspace/node_modules/pg/lib/query.js:143:17)
at Connection.connectedErrorHandler (/home/ubuntu/workspace/node_modules/pg/lib/client.js:132:26)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Socket.<anonymous> (/home/ubuntu/workspace/node_modules/pg/lib/connection.js:123:12)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
Aucun commentaire:
Enregistrer un commentaire