2017-11-28 1 views
0

PostgreSQLのテーブルに格納されているlargeObjectのデータの行をプルする選択クエリがあります。フィールド内の特定のデータは、htmlファイルです。選択クエリの結果セットのデータフィールドから特定の内容を返します。

データフィールドの名前を16543(16543kBの場合)と表示されるコンソールに出力できます。

私の書き込みの問題は、実際のコンテンツ(HTML)をどのように返して、後でそれを1つのオブジェクトとしてエクスポートしてブラウザに送ることができるかということです。

私はノードを使用して、これまでHERESに私のソースコードを表現しています:

var database = require('../database/postgresDB.js'); 
var pg = require('pg'); 
var html = {}; 

var connectionString = "postgres://dabladmin:[email protected]:5432/dablpatient"; 

var client = new pg.Client(connectionString); 
client.connect(); 

var query = client.query('SELECT * FROM htmlfiles WHERE id = 1', function(err, result){ 
     console.log(JSON.stringify(result)); 
     console.log(result.rows[0].htmlfile); 
     html = result.rows[0].htmlfile; 
     //return result.rows[0].htmlfile; 
     //console.dir(html); 
}); 

module.exports = html; 

答えて

1

これを直接実行することはできません。約束を返す関数をエクスポートする必要があります。

これはどのように行うことができるかという考え方です。注:コードはテストされていません。

some.controller.jsインサイド
// htmlfile.model.js 
const promise = require('bluebird'); // or any other Promise/A+ compatible library; 
const initOptions = { 
    promiseLib: promise // overriding the default (ES6 Promise); 
}; 

const pgp = require('pg-promise')(initOptions); 

// Database connection details; 
const cn = { 
    host: 'localhost', // 'localhost' is the default; 
    port: 5432, // 5432 is the default; 
    database: 'myDatabase', 
    user: 'myUser', 
    password: 'myPassword' 
}; 
const db = pgp(cn); // database instance; 

const getHtml = id => db.oneOrNone('SELECT * FROM htmlfiles WHERE id = $1', id); 

module.exports = getHtml; 

const html_model = require('./htmlfile.model.js'); 

html_model.getHtml(1) 
    .then(data => { 
     if(data) { 
      // record found 
      res.send(data.htmlfile); 
     } else { 
      // record not found, do something else 
     } 
    }) 
    .catch(error => { 
     // an error occurred 
    }); 
+0

オーケーは、私はあなたが私はそれを試してみるよ、ここで何をしているかを理解していただきありがとうございます! – Llanod11

+0

TypeError:html_model.getHtmlは関数ではありません – Llanod11

+0

これをルータディレクトリファイルに渡しましたので、結果をブラウザに送信できます:router.get( '/ new'、function(req、res、next){ html_model。 getHtml()。then(result => { res.send(result.rows [0] .htmlfile); }); }); – Llanod11

関連する問題