0

I want to use the idm_db package with Typescript and React. In its document, it only explains nodejs and also it does not support types. Luckily, there is @types/ibm_db and I think it supports types, but I cannot figure out how to use this one.

In nodejs,

var ibmdb = require('ibm_db');

ibmdb.open("DATABASE=<dbname>;HOSTNAME=<myhost>;UID=db2user;PWD=password;PORT=<dbport>;PROTOCOL=TCPIP", function (err,conn) {
  if (err) return console.log(err);

  conn.query('select 1 from sysibm.sysdummy1', function (err, data) {
    if (err) console.log(err);
    else console.log(data);

    conn.close(function () {
      console.log('done');
    });
  });
});

node ibm_db

Following this one, I just changed first line, from var ibmdb = require('ibm_db'); to import * asibmdb from 'ibm_db'. But, it keeps throwing an error related to webpack.(I don't use webpack at this point). It compiles, but page is showing the error messages.

So, my question is, how can I use this package in React and Typescript way? Thanks in advance!

EDIT It throws lots of errors and I am posting errors that I think it may cause.

  28 |                      path.resolve(__dirname, '../installer/clidriver/lib');
  29 | }
  30 | 
> 31 | var odbc = require("bindings")("odbc_bindings")
  32 |   , SimpleQueue = require("./simple-queue")
  33 |   , util = require("util")
  34 |   , Readable = require('stream').Readable
199 |   // Avoids an infinite loop in rare cases, like the REPL
  200 |   dir = process.cwd();
  201 | }
> 202 | if (
      | ^  203 |   exists(join(dir, 'package.json')) ||
  204 |   exists(join(dir, 'node_modules'))
  205 | ) {
  147 |         );
  148 |         hotCurrentParents = [];
  149 |     }
> 150 |     return __webpack_require__(request);
      | ^  151 | };
  152 | var ObjectFactory = function ObjectFactory(name) {
  153 |     return {
jayko03
  • 1,737
  • 5
  • 22
  • 38
  • 2
    What error are you getting? "keeps throwing an error", what is that error? – basic Dec 10 '19 at 20:45
  • @basic I added some errors on the page – jayko03 Dec 10 '19 at 21:39
  • How did you setup your project? I am actually experiencing other issues. `npx create-react-app jayko-app --template typescript` and then `cd jayko-app npm start` works fine. Once I installed `npm install ibm_db` and did nothing else, I started getting a react declaration file error. – chrisbyte Dec 10 '19 at 23:26
  • @chrisbyte I started with `create-reat-app --typescript`. I also had `npm install --save-dev ibm_db` and `npm install @types/ibm_db --save`. I think the declaration file error is from typescript. pure ibm_db does not have types. So I installed `@types/ibm_db`. – jayko03 Dec 11 '19 at 02:07

1 Answers1

0

For something like this, I might use

import * as ibmdb from 'ibm_db';

Does that help? Some reference: https://stackoverflow.com/a/43172907/8678978

I have the same issue when I use the moment package in React with typescript

chrisbyte
  • 1,061
  • 1
  • 8
  • 14