cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

client.openSession() : TypeError: Cannot read properties of undefined (reading '0')

slloyd
New Contributor

I am using the Databricks SQL Driver for Node.js to create an endpoint that queries a Databricks database following the guide here Databricks SQL Driver for Node.js | Databricks on AWS . This code was working previously but now I am getting a TypeError: Cannot read properties of undefined (reading '0') when executing 

session = await client.openSession(); I have verified my environment variables are correct.
api/databricks/route.ts

 

export async function POST(req: NextRequest) {  
  console.log("initialized");  
  const body = await req.json();  
  const { query } = body;  
  console.log("Received Query:", query);  
  
  if (token == "" || serverHostname == "" || httpPath == "") {  
    return NextResponse.json(  
      {  
        error:  
          "Cannot find Server Hostname, HTTP Path, or personal access token. " +  
          "Check the environment variables DATABRICKS_SERVER_HOSTNAME, " +  
          "DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN.",  
      },  
      { status: 500 }  
    );  
  }  
  
  const client = new DBSQLClient();  
  const connectOptions = {  
    host: serverHostname,  
    token: token,
    path: httpPath,
  };  
  
  console.log("Connect Options:", connectOptions);  
  
  const timeout = 30000; // Timeout in milliseconds (30 seconds)  
  try {  
    await client.connect(connectOptions);  
    console.log("Connected to Databricks");

    let session: IDBSQLSession;
    try {  
      session = await client.openSession();  
      console.log("Session opened");  
    } catch (openSessionError) {  
      console.error("Error opening session:", openSessionError);  
      throw openSessionError; // Rethrow to be caught by the outer catch  
    } 
  
    const statements = splitSQLStatements(query);  
    let result: any[] = [];  
  
    for (const statement of statements) {  
      console.log("Executing Statement:", statement);  
      const queryOperationPromise: IOperation = await session.executeStatement(  
        statement,  
        {  
          runAsync: true,  
          maxRows: 500, // This option enables the direct results feature.  
        }  
      );  
  
      let queryOperation: IOperation;  
      try {  
        queryOperation = (await Promise.race([  
          queryOperationPromise,  
          createTimeoutPromise(timeout),  
        ])) as IOperation;  
      } catch (error) {  
        if (error instanceof Error && error.message.includes("timed out")) {  
          console.error("Error: Query execution timed out.");  
          return NextResponse.json(  
            { error: "Query execution timed out." },  
            { status: 500 }  
          );  
        } else {  
          throw error;  
        }  
      }  
  
      const statementResult = await queryOperation.fetchAll();  
      console.log(  
        "Result for Statement:",  
        util.inspect(statementResult, { depth: null })  
      );  
  
      // Check if the result is not empty and merge the results  
      if (statementResult.length > 0) {  
        result = result.concat(statementResult);  
      } else {  
        console.warn(`No data returned for statement: ${statement}`);  
      }  
      await queryOperation.close();  
    }  
  
    await session.close();  
    client.close();  
    console.log("Final Result:", util.inspect(result, { depth: null }));  
  
    // Ensure the result is serializable  
    return NextResponse.json(result);  
  } catch (error) {  
    if (error instanceof Error) {  
      console.error("Error executing query: ", error);  
      return NextResponse.json(  
        { error: "Failed to execute query: ", details: error.message },  
        { status: 500 }  
      );  
    }  
  }  
}  

 

console:

 

POST /api/chat 200 in 13099ms
initialized
Received Query: use catalog iedatalakeprd;
use schema ds_cmdb;

SELECT
    win.Name AS Server_Name,
    win.OperationalStatus AS Operational_Status,
    loc.Name AS Location_Name,
    dep.Name AS Department_Name
FROM
    silver_cmdbciwinserver AS win
LEFT JOIN
    silver_cmnlocation AS loc ON win.Location_Id = loc.Id
LEFT JOIN
    silver_cmndepartment AS dep ON win.ElmoDepartment = dep.Id;
{"level":"info","message":"Created DBSQLClient"}
Connect Options: {
  host: 'adb-6894947688059139.19.azuredatabricks.net',
  token: 'dapid12947cad444f67eb7d44ff57f4148b3',
  path: '/sql/1.0/warehouses/e3042c2a5aa08bc9'
}
Connected to Databricks
Error opening session: TypeError: Cannot read properties of undefined (reading '0')
    at isInsideNodeModules (node:internal/util:508:17)
    at showFlaggedDeprecation (node:buffer:178:8)
    at new Buffer (node:buffer:266:3)
    at new module.exports (webpack-internal:///(rsc)/./node_modules/node-int64/Int64.js:65:34)
    at DBSQLClient.eval (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:197:111)
    at Generator.next (<anonymous>)
    at eval (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:31:71)
    at new Promise (<anonymous>)
    at __awaiter (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:27:12)
    at DBSQLClient.openSession (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:196:16)
    at POST (webpack-internal:///(rsc)/./app/api/databricks/route.ts:73:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /home/slloyd/database_ai_assistant/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:53446
    at async e_.execute (/home/slloyd/database_ai_assistant/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:44747)
    at async e_.handle (/home/slloyd/database_ai_assistant/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:54700)
    at async doRender (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1377:42)
    at async cacheEntry.responseCache.get.routeKind (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1599:28)
    at async DevServer.renderToResponseWithComponentsImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1507:28)
    at async DevServer.renderPageComponent (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1924:24)
    at async DevServer.renderToResponseImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1962:32)
    at async DevServer.pipeImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:920:25)
    at async NextNodeServer.handleCatchallRenderRequest (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:816:17)
    at async /home/slloyd/database_ai_assistant/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/home/slloyd/database_ai_assistant/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/start-server.js:141:13)
Error executing query:  TypeError: Cannot read properties of undefined (reading '0')
    at isInsideNodeModules (node:internal/util:508:17)
    at showFlaggedDeprecation (node:buffer:178:8)
    at new Buffer (node:buffer:266:3)
    at new module.exports (webpack-internal:///(rsc)/./node_modules/node-int64/Int64.js:65:34)
    at DBSQLClient.eval (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:197:111)
    at Generator.next (<anonymous>)
    at eval (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:31:71)
    at new Promise (<anonymous>)
    at __awaiter (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:27:12)
    at DBSQLClient.openSession (webpack-internal:///(rsc)/./node_modules/@databricks/sql/dist/DBSQLClient.js:196:16)
    at POST (webpack-internal:///(rsc)/./app/api/databricks/route.ts:73:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /home/slloyd/database_ai_assistant/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:53446
    at async e_.execute (/home/slloyd/database_ai_assistant/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:44747)
    at async e_.handle (/home/slloyd/database_ai_assistant/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:54700)
    at async doRender (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1377:42)
    at async cacheEntry.responseCache.get.routeKind (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1599:28)
    at async DevServer.renderToResponseWithComponentsImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1507:28)
    at async DevServer.renderPageComponent (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1924:24)
    at async DevServer.renderToResponseImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:1962:32)
    at async DevServer.pipeImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:920:25)
    at async NextNodeServer.handleCatchallRenderRequest (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/base-server.js:816:17)
    at async /home/slloyd/database_ai_assistant/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/home/slloyd/database_ai_assistant/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/home/slloyd/database_ai_assistant/node_modules/next/dist/server/lib/start-server.js:141:13)
 POST /api/databricks 500 in 189ms

 

0 REPLIES 0

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group