为什么我无法成功运行node-postgres?

时间:2019-05-05 00:13:43

标签: node-postgres

我正在尝试使用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”变量,它要求我填写用户名和密码。

0 个答案:

没有答案