2017-10-09 43 views
4

我正在嘗試整合node-postgres驅動程序並學習做一個簡單的CRUD操作。在我app.js,我做這樣的事情:節點 - 使用node-postgres設置Postgres驅動程序

... 
var postgres = require('./adapters/postgres') 
var postClient = new postgres(conf); 
... 
postClient.connect(function (dbconn) { 
    app.dbconn = dbconn; 
    app.conf = conf; 

    console.log("************************************************************"); 
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']); 
    console.log("************************************************************"); 

    server.listen(conf['web']['port']); 

    var Routes = require('./routes/http-routes'); 
    new Routes(app); 
}); 

裏面我adapters/postgres.js文件,我有以下內容:

const Client = require('pg'); 
const postClient = new Client(conf)({ 
    host: conf['postgres'].host, 
    port: conf['postgres'].port, 
    dbname: conf['postgres'].dbname, 
    username: conf['postgres'].username, 
    password: conf['postgres'].password, 
    dbconn: null, 
}); 
module.exports = postClient; 
postClient.prototype.connect = function (cbk) { 
    var self = this; 
    client.connect(function (err, db) { 
     console.log(new Date() + " | Postgres Server Connection Establised..."); 
     console.log(new Date() + " | Current database: ", db.databaseName); 
     if (!err) { 
      console.log(new Date() + " | Postgres Server Authenticated..."); 
      self.dbconn = db; 
      cbk(db); 
     } else { 
      console.log(new Date() + " | Postgres Server Error in connection..."); 
      console.log(err); 
      self.dbconn = db; 
      cbk(db); 
     } 
    }); 
}; 

與上面的代碼,我不斷收到此錯誤:ReferenceError: conf is not defined所以我加它作爲var conf = require('../config/conf');。這不是一個合適的解決方案,因爲我想從app.js中通過它。接下來,即使添加了這個,我也會得到以下錯誤:TypeError: Client is not a constructor。有人可以指導解決這兩個錯誤?

+0

@brianc任何幫助將不勝感激! – JackSlayer94

+0

一個很好的例子:[使用Node和Postgres設計一個RESTful API](http://mherman.org/blog/2016/03/13/designing-a-restful-api-with-node-and-postgres/)。 –

+0

@ vitaly-t謝謝,我會用它作爲基礎!爲了理解,你可以幫助我使用當前的代碼嗎? – JackSlayer94

回答

2

導出一個工廠函數,它接受配置對象並返回客戶端的一個實例。 Client不是pg中的默認導出,因此需要對其進行解構。

const { Client } = require('pg'); 

const createPgClient = (conf) => { 
    const pgClient = new Client(conf)({ 
     host: conf['postgres'].host, 
     port: conf['postgres'].port, 
     dbname: conf['postgres'].dbname, 
     username: conf['postgres'].username, 
     password: conf['postgres'].password, 
     dbconn: null, 
    }); 

    pgClient.prototype.connect = function (callback) { 

     client.connect() 
     .then(() => { 
      console.log(new Date() + " | Postgres Server Authenticated..."); 
     }) 
     .catch((err) => { 
      console.log(new Date() + " | Postgres Server Error in connection..."); 
      console.log(err); 
     }) 
     .finally(()=> { 
      self.dbconn = this; 
      callback(this); 
     }); 

    }; 
    return pgClient; 
}; 
module.exports = createPgClient; 

在您的app.js中使用此工廠函數來創建客戶端。

const createPgClient = require('./adapters/postgres') 
cont pgClient = createPgClient(conf); 

pg.connect(function (dbconn) { 
    app.dbconn = dbconn; 
    app.conf = conf; 

    console.log("************************************************************"); 
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']); 
    console.log("************************************************************"); 

    server.listen(conf['web']['port']); 

    const Routes = require('./routes/http-routes'); 
    new Routes(app); 
}); 
+0

隨着上面貼出的代碼,我得到以下錯誤: 'var pgClient = createPgClient(conf); ^ ReferenceError:createPgClient未定義 – JackSlayer94

相關問題