Node.js - Oracle驱动程序 - 检索Clob字段

时间:2015-12-11 05:04:29

标签: node.js express node-oracle

我正在尝试使用Oracle Driver(1.4.0)为NodeJS-4.2.2,Express(4.13.3)从Oracle DB中提取数据。

我能够将输出写入流(文件),但无法将其分配给变量对象。以下是代码段。无论如何,我将clob字段输出分配给var?

{
                    if (err) { console.error(err.message); return; }
                    if (result.rows.length === 0) { console.log("No results"); return; }

                    var clobe = "";
                    var lob = result.rows[2][1];
                    if (lob === null) { console.log("BLOB was NULL"); return; }

                   // lob.setEncoding('utf8');
                    lob.on(
                        'end',
                        function()
                        {
                            console.log("lob.on 'end' event");
                            res.end();
                        });
                    lob.on(
                        'close',
                        function()
                        {
                            console.log("lob.on 'close' event");
                            connection.release(function(err) {
                                if (err) console.error(err);
                            });
                        });
                    lob.on(
                        'error',
                        function(err)
                        {
                            console.log("lob.on 'error' event");
                            console.error(err);
                        });
                    lob.on('data', function (chunk) { clobe += chunk; console.log("Added chunk("+chunk.length+") to clob("+clobe.length+")");});


                    res.writeHead(200, {'Content-Type': 'application/json' });

                    lob.pipe(res);
                }

2 个答案:

答案 0 :(得分:0)

    if (lob === null) { 
      console.log("CLOB was NULL");
      return;
    }
    var clob = "";
    lob.setEncoding('utf8');  // we want text, not binary output

    lob.on('end', function() {
      console.log("lob.on 'end' event ", clob);
    });
    lob.on('close', function() {
      console.log("lob.on 'close' event");
    });
    lob.on('error', function(err) {
      console.log("lob.on 'error' event");
      console.error(err);
    });
    lob.on('data', function (chunk) { 
      clob += chunk;
      console.log("Added chunk("+chunk.length+") to clob("+clob.length+")");
    });

答案 1 :(得分:0)

使用

  oracledb.fetchAsString = [ oracledb.CLOB ];

,您的CLOB将作为字符串检索

Docs