JSON.parse 在 Node.js 中的 Oracle DB 结果集失败

JSON.parse is failing for resultset from Oracle DB in Node.js

按照Demo: GraphQL with Oracle Database and node-oracledb中提供的解决方案,我使用 Apollo GraphQL 创建了一个简单的 Node JS 应用程序。但是,我坚持将从 Oracle DB 获得的结果集转换为 GraphQL 模式所期望的形式。如果 我在结果集上使用 JSON.Parse,如上面提到的 link 所示,它抛出错误 -

SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse () at Object.getAssets.. executeQuery.js:39:19

这是我在 Node.js 中的第一个代码,我仍在学习中。因此,有人可以帮助我理解为什么它对我不起作用。我正在记录从 DB 获得的结果,它按预期返回了 10 条记录。

这是我的代码:

GraphQL 模式定义:schema.js

const { gql } = require("apollo-server");

const typeDefs = gql`
    type Asset {
        id: ID!
        account_id: String
        group_id: String            
    }
    
    type Query {
        assets: [Asset]
    }
    `;

module.exports = typeDefs;

解析器:resolvers.js

const queryrunner = require("./executeQuery.js")

const resolvers = {
    Query: {
        assets: async() => queryrunner.getAssets(),
    },
};

module.exports = resolvers;

正在从文件中获取连接 oracledatasource.js

let connection;
var oracledb = require('oracledb');

module.exports = {
  getConnection : function () {
      
    connection = oracledb.getConnection({
    user          : "username",
    password      : "password",
    connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))(CONNECT_DATA =(SERVICE_NAME = servicename)))"
  });
  console.log("Successfully connected to Oracle!")
  return connection;
  }
}

并在 executeQuery.js 中执行查询:

    const conn = require('./oracledatasource')

    module.exports = {

    getAssets : async() => {
      let connection = await conn.getConnection();
      let query = "SELECT\r\n"
            + "    CA.ASSET_ID as id,\r\n"
            + "    CA.ACCNT_ID as account_id,\r\n"
            + "    CA.GROUP_ID as group_id\r\n"         
            + "FROM\r\n"
            + "    SCHEMA.TABLE CA\r\n"
            + "WHERE\r\n"
            + "        CA.ACTIVE_FLG = 1\r\n"
            + "    AND ROWNUM <= 10";

      console.log(query);
      let result = await connection.execute(query, []);
      connection.release(); 
      
      console.log(result.rows);
      
      return JSON.parse(result.rows);
    }      
  }

index.js 是我的主文件:

const { ApolloServer } = require("apollo-server");
    
const typeDefs = require('./schema');
    
const resolvers = require('./resolvers');
    
const server = new ApolloServer({
    typeDefs,
    resolvers
    //mocks: true
});
    
server.listen().then(({url}) =>
    console.log(`Server running at ${url}`)
);

针对此问题给出的解决方案 - Question 有助于获得所需的答复。

我按照建议遵循了本手册 - node-oracledb 并将 outformat 传递给 execute() 函数,该函数将响应作为对象数组返回。这消除了使用 JSON.parse.

的需要
  const conn = require('./oracledatasource')
  var oracledb = require('oracledb');

  module.exports = {

    getAssets : async() => {
      let connection = await conn.getConnection();
      let query = "SELECT\r\n"
            + "    CA.ASSET_ID \"id\",\r\n"
            + "    CA.ACCNT_ID \"account_id\",\r\n"
            + "    CA.GROUP_ID \"group_id\"\r\n"
            + "FROM\r\n"
            + "    SCHEMA.TABLE CA\r\n"
            + "WHERE\r\n"
            + "        CA.ACTIVE_FLG = 1\r\n"
            + "    AND ROWNUM <= 10";

      console.log(query);
      let result = await connection.execute(query, [], { outFormat: oracledb.OUT_FORMAT_OBJECT });
      connection.release(); 
      
      console.log(result.rows);
      
      return Object.values(result.rows);      
    }      
  }