2017-11-28 1 views
0

PostgreSQL 테이블에 저장된 largeObject의 데이터 행을 가져 오는 select 쿼리가 있습니다. 해당 필드의 특정 데이터는 html 파일입니다.select 쿼리의 결과 집합에있는 데이터 필드에서 특정 내용을 반환합니다.

데이터 필드의 이름을 16543 (16543kB의 경우)으로 표시되는 콘솔에 출력 할 수 있습니다.

그래서 내 불타는 질문은 실제 내용 (HTML)을 반환하여 하나의 개체로 내 보낸 다음 브라우저로 보낼 수 있도록하는 것입니다.

내가 노드를 사용하여 지금까지를 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

이 직접 수행 할 수 없습니다. 약속을 되 돌리는 함수를 export 할 필요가있다.

다음은 어떻게 수행 할 수 있는지에 대한 아이디어입니다. 참고 : 코드는 테스트되지 않았습니다.

// 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; 

some.controller.js 내부의

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 (결과 => { res.send (result.rows [0] .htmlfile); }); }); – Llanod11

관련 문제