从选择查询的结果集中的数据字段返回特定内容

时间:2017-11-28 09:36:07

标签: node.js postgresql express

我有一个select查询,它从存储在PostgreSQL表中的largeObject中提取一行数据。该字段中的特定数据是一个html文件。

我可以将数据字段的名称输出到控制台,显示为16543(16543kB)。

所以,我的问题是我如何返回实际内容(html),以便我可以随后将其作为一个对象导出并将其发送到浏览器。

到目前为止,我正在使用节点并表达heres我的源代码:

// Server side:
function TServerMethodsMain.GetDataSetRecords(const ADataInfo: String): TFDJSONDataSets;
begin
  Result := TFDJSONDataSets.Create;
  // ... some codes here to find dataset according to ADataInfo
  TFDJSONDataSetsWriter.ListAdd(Result, 'Table1', TFDAdaptedDataSet(FDTable1));
end;

// Client side:
procedure udxGetData(ADataSet: TFDDataSet; ADataInfo: String);
var
  LDataSetList: TFDJSONDataSets;
begin
  ADataSet.Close;
  LDataSetList := ClientModuleMain.ServerMethodsMainClient.GetDataSetRecords(ADataInfo);
  ADataSet.Data := TFDJSONDataSetsReader.GetListValue(LDataSetList, 0);
  ADataSet.Open;
end;

1 个答案:

答案 0 :(得分:1)

这不能直接完成。您需要导出一个将返回承诺的函数。

以下是如何做到这一点的想法。注意:代码未经过测试。

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