Почему я не могу успешно запустить node-postgres? - PullRequest
0 голосов
/ 05 мая 2019

Я пытаюсь создать простое приложение с полным стеком, используя node и postgres. Однако, похоже, что в моем коде много проблем! Пожалуйста, помогите мне, как это понять!

код в моем файле app.js

var express = require("express");
    path = require("path"),
    bodyparser = require("body-parser"),
    cons = require("consolidate"),
    dust = require("dustjs-helpers"),
    app = express();
const { Pool, Client } = require('pg')
// DB connect string
var connect = "postgres://PostgreSQL11:23890WEUIop@database.server.com:5432/recipe";

//assign dust engine to .dust files
app.engine('dust',cons.dust);

//set default ext
app.set('view engine','dust');
app.set('views',__dirname + '/views');
//set public folder
app.use(express.static(path.join(__dirname,'public')));

// bodyparser middleware
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:false}));


app.get('/',(req,res)=>{
    const pool = new Pool({
        connectionString: connect,
      })

      pool.query('SELECT NOW()', (err, res) => {
        console.log(err, res)
        pool.end()
      })

      const client = new Client({
        connectionString: connect,
      })
      client.connect()

      client.query('SELECT * FROM recipes;', (err, res) => {
        console.log(err, res)
        res.render('index',{recipes: res.rows});
        client.end()
      })
});

app.listen(3000, function(){
    console.log('Server Started on Port 3000')
});

Вот что показывает мой терминал:

Jiatongs-MacBook-Pro:recipeAPP jiatongli$ node app.js 
Server Started on Port 3000
(node:39957) UnhandledPromiseRejectionWarning: error: role "riederlee" does not exist
    at Connection.parseE (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/connection.js:601:11)
    at Connection.parseMessage (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/connection.js:398:19)
    at Socket.<anonymous> (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/connection.js:120:22)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:39957) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:39957) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Error: Connection terminated unexpectedly
    at Connection.con.once (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/client.js:223:9)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/connection.js:130:10)
    at Socket.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1103:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) undefined
/Users/jiatongli/Desktop/recipeAPP/app.js:42
        res.render('index',{recipes: res.rows});
            ^

TypeError: Cannot read property 'render' of undefined
    at Query.client.query [as callback] (/Users/jiatongli/Desktop/recipeAPP/app.js:42:13)
    at Query.handleError (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/query.js:142:17)
    at process.nextTick (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/client.js:61:13)
    at process._tickCallback (internal/process/next_tick.js:61:11)
[1]+  Killed: 9               node app.js
Jiatongs-MacBook-Pro:recipeAPP jiatongli$ node app.js 
Server Started on Port 3000
(node:40274) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND database.server.com database.server.com:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
(node:40274) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:40274) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
{ Error: getaddrinfo ENOTFOUND database.server.com database.server.com:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'database.server.com',
  host: 'database.server.com',
  port: 5432 } undefined
Error: Connection terminated unexpectedly
    at Connection.con.once (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/client.js:223:9)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/connection.js:76:10)
    at Socket.emit (events.js:189:13)
    at TCP._handle.close (net.js:600:12) undefined
/Users/jiatongli/Desktop/recipeAPP/app.js:42
        res.render('index',{recipes: res.rows});
            ^

TypeError: Cannot read property 'render' of undefined
    at Query.client.query [as callback] (/Users/jiatongli/Desktop/recipeAPP/app.js:42:13)
    at Query.handleError (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/query.js:142:17)
    at process.nextTick (/Users/jiatongli/Desktop/recipeAPP/node_modules/pg/lib/client.js:61:13)
    at process._tickCallback (internal/process/next_tick.js:61:11)
Jiatongs-MacBook-Pro:recipeAPP jiatongli$ 

Что здесь произошло? Так как я впервые использую pg, у меня недостаточно опыта, чтобы это понять. Плюс для переменной «connect», он просит меня ввести имя пользователя и пароль.

...