node-js 的 oracledb 节点(几个查询一个接一个)。另一个将结果作为绑定参数

oracledb node for node-js (several queries one after the other). The other take result as binds parameter

我被困在我的问题上,因为我对 node-js 的 oracle-db 和一般的 JavaScript 真的很陌生。 我用 JavaScript 编写了 API-endpoints,每个端点都执行 oracledb “select 查询”。但是,我的同事只需要一个端点,它将所有 oracle select 查询的所有结果作为一个 JSON 对象提供。所有查询都依赖于第一个查询,即第一个查询的结果必须用作第二个查询的绑定参数,依此类推。 在这个 js 文件中,我尝试执行两个 oracle-sql 查询(一个接一个)并使用第一个查询的结果-JSON-对象的一个​​值作为第二个查询的参数并将所有结果作为对 API 客户端的响应。但这是行不通的。我在这里做错了什么?只有一个 sql,因此只有一个结果有效。提前致谢!

这是我写的代码:


const oracledb = require('oracledb');
const config = require('../config/config')

oracledb.autoCommit = true
oracledb.fetchAsString = [ oracledb.DATE, oracledb.NUMBER ]


module.exports= async (req, res) => {

   
   let connection;

 try {

   var sql, binds, options, result, result2, time

   connection = await oracledb.getConnection(config.oracledbconnection)

   // Query the data 


   sql = `SELECT FIRSTNr, SECONDNR,STARTTIME,MACHINE FROM MACHINELOGS WHERE Machine=:Machine AND ENDTIME > CURRENT_DATE -1 ORDER BY RUNNO ASC`;
   
   binds = {Machine:req.params.machine}

   options = {
     outFormat: oracledb.OUT_FORMAT_OBJECT // query result format 
     //outFormat: oracledb.OBJECT // is the same as above // extendedMetaData: true, // get extra metadata 
     // prefetchRows: 100, // internal buffer allocation size for tuning 
     // fetchArraySize: 100 //internal buffer allocation size for tuning 
   };
   
   result = await connection.execute(sql, binds, options);

   // console.log("Metadata: ");
   // console.dir(result.metaData, { depth: null });
   // console.log("Query results: ");
   // console.dir(result.rows, { depth: null });
   


   // 
   // Show the date. The value of ORA_SDZT affects the output

   sql = `SELECT TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD FROM DUAL`;
   time = await connection.execute(sql, binds, options);


   // console.log("Current date query results: ");
   // console.log(result2.rows[0]['CD']);

   
   sql = `SELECT GRADE FROM NOTES WHERE SECONDNR=:Secondnr`
   binds = {Secondnr:result.rows[0]['SECONDNR']}
   result2 = await connection.execute(sql,binds,options);
   options = {
     outFormat: oracledb.OUT_FORMAT_OBJECT // 
   };

   

 } 
 
 catch (err) 
 {
   console.error(err);
 } 
 
 finally 
 {
   if (connection) 
   {
     try {
       await connection.close();
     } 
     catch (err) {
       console.error(err);
     }
   }
 }
 res.send(result,result2) 
 
   
}

回归本源。这有效:

'use strict';

process.env.ORA_SDTZ = 'UTC';

const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');

if (process.platform === 'darwin') {
  oracledb.initOracleClient({libDir: process.env.HOME + '/Downloads/instantclient_19_8'});
}

let sql, options, result1, result2;
options = { outFormat: oracledb.OUT_FORMAT_OBJECT };

async function run() {
  let connection;

  try {
    connection = await oracledb.getConnection(dbConfig);

    result1 = await connection.execute(
      `select 1 as myval from dual`,
      [], options);
    console.dir(result1.rows, { depth: null });

    result2 = await connection.execute(
      `select sysdate from dual where 1 = :bv`,
      {bv: result1.rows[0].MYVAL},
      options);
    console.dir(result2, { depth: null });

  } catch (err) {
    console.error(err);
  } finally {
    if (connection) {
      try {
    await connection.close();
      } catch (err) {
    console.error(err);
      }
    }
  }
  
}

run();

然后尝试使用您的数据类型。

我不确定你想要返回 'all results' 做什么。您可以构建任何 JS 对象并使用 JSON.stringifyJSON.parse.

轻松地与 JSON 相互转换

为了提高效率,您可以查看是否可以在一个 SQL 语句中完成所有 'queries'。