ibmdb / node-ibm_db

IBM DB2 and IBM Informix bindings for node
MIT License
190 stars 151 forks source link

Nextjs issue with indexOf #946

Closed dominikS007 closed 1 year ago

dominikS007 commented 1 year ago

Hello

So I wanted to implement the connection between the site that I am building and the server that I have to use, I started to build the website in Nextjs 13 using app router. When making some of the front end i wanted to connect it to the db, I have used the package before in a different project and it worked, that project used ExpressJS and there were no issues at all. Unfortunately this is not the case with NextJS. After installed the package with npm and tying to connect to the db and display the results in the API i get the following error,

Server Error
TypeError: Cannot read properties of undefined (reading 'indexOf')

This error happened while generating the page. Any console logs will be displayed in the terminal window.

image

Steps to Reproduce:

  1. start a new repo of nextJS and all of the configurations that it asks are default
  2. added the npm package for ibmdb2
  3. Created the following directories/files in the app dir

File 1


src/app/api/db/route.ts

In the route.ts file add the following code

import { NextResponse } from "next/server";
import connectToDb from "@/app/lib/db.js";

export async function GET(request: Request) {

    /*const data = await containerIssueQuery();*/
    try{
        const data = await connectToDb();
        return NextResponse.json({body: data}, {status: 200});
    }catch (e) {
        return NextResponse.json({body: e}, {status: 500});
    }

}

File 2


src/app/lib/db.js

I have the correct connection string and sql query as I tried to just run it with the following command and it worked

node /[file path]
import * as ibmdb from "ibm_db"

const connectionString = `DATABASE=${''};HOSTNAME=${''};UID=${''};PWD=${''};PORT=${''};PROTOCOL=${''};`

export default function connectToDb() {
    db.open(connectionString, function (err, conn) {
        if (err) return console.log(err);
        const sql = ``;
        conn.query(sql, function (err, data) {
            if (err) console.log(err);
            conn.close(function () {});
            return data
        });
    });
};

To get the error now just have to go to the browser and type in http://localhost:3000/api/db and you should get the same error.

Is there a way to resolve this issue or am i doing something wrong that I am not noticing?

The error that gets displayed in the console

- error node_modules\bindings\bindings.js (178:15) @ indexOf
- error Error [TypeError]: Cannot read properties of undefined (reading 'indexOf')
    at Function.getFileName (webpack-internal:///(sc_server)/./node_modules/bindings/bindings.js:192:18)
    at bindings (webpack-internal:///(sc_server)/./node_modules/bindings/bindings.js:130:52)
    at eval (webpack-internal:///(sc_server)/./node_modules/ibm_db/lib/odbc.js:38:98)
    at Object.(sc_server)/./node_modules/ibm_db/lib/odbc.js 
...
null
bimalkjha commented 1 year ago

@dominikS007 It looks similar to a closed issue #842. Please go through it and try the diagnostics. If node test-basic-test.js works for you after updating database connection info in file config.testConnectionStrings.json, it is issue with NextJS which fails to load the odbc_bindings.node file and you need to debug bindigs.js to know the root cause. Thanks.

VishnuKugarthi commented 7 months ago

Hi @dominikS007, I'm facing the same issue. Were you able to resolve this? If so please let me know how you have resolved it. Thanks

@bimalkjha