0
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var oracledb = require('oracledb');
const morgan = require('morgan');

app.use(morgan("combined"));

app.use(bodyParser.json());

 app.post("/user", function (req, res){
     console.log("Posting data into table :");
     const connection = oracledb.getConnection(
        {
          user          : "SYSTEM",
          password      : "************",
          connectString : "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=DESKTOP-*****)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)))"
        })
        .then(function(conn) {
          return conn.execute(
            `INSERT INTO table1 (department_id, department_name) VALUES` [req.body.department_id, req.body.department_name]
          )
            .then(function(result) {
              console.log(result.rows);
              res.json(result.rows);
              return conn.close();
            })
            .catch(function(err) {
              console.error(err);
              return conn.close();
            });
        })
        .catch(function(err) {
          console.error(err);
        });
    connection.release(
    function (err) {
        if (err) {
            console.error(err.message);
        } else {
            console.log("POST /user_profiles : Connection released");
        }
    });
});
 app.listen(3000, ()=>{
     console.log("Connected . . . ")
 })

I am unable to release the connection in the POST program written above, the error is shown as follows :

Error: NJS-006: invalid type for parameter 1 at Object.assert (C:\Node\NodePrograms\node_modules\oracledb\lib\util.js:80:11) at Connection.execute (C:\Node\NodePrograms\node_modules\oracledb\lib\connection.js:129:12) at C:\Node\NodePrograms\node_modules\oracledb\lib\util.js:116:16 at new Promise () at Connection.execute (C:\Node\NodePrograms\node_modules\oracledb\lib\util.js:104:14) at C:\Node\NodePrograms\postdb.js:20:23

I am using ARC of chrome to post the data.Its showing 500 Internal Server Error and <pre>TypeError: connection.release is not a function I am using Node JS and Oracle DB.

MT0
  • 143,790
  • 11
  • 59
  • 117
Jishnu
  • 33
  • 1
  • 7
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Evert Sep 20 '18 at 15:14

1 Answers1

2

Seems there are two different errors here. Let's look at connection.release is not a function first.

oracledb.getConnection is an async function. It returns undefined if the last parameter is a callback function or a promise if the last parameter is not a callback function. In either case, it does not return a connection directly.

Since you didn't pass in a callback function as the last parameter, it's returning a promise. The promise doesn't have a release method, hence the error you get when invoking connection.release. However, you are already closing the connection with conn.close in another code path.

As for NJS-006: invalid type for parameter 1 at Object.assert, this is related to your call to conn.execute (as indicated in the error stack). Here's what you have:

conn.execute(
  `INSERT INTO table1 (department_id, department_name) VALUES` [req.body.department_id, req.body.department_name]
)

Notice that you're passing a single value (an expression) to execute, not two parameters as you may have intended (missing the comma). The template literal and the array can result in weird values:

console.log(`string` [1]); // "t"
console.log(`string` [{}]); // undefined

You're likely passing undefined to conn.execute. This is one of those "Wat?" moments. :) https://www.destroyallsoftware.com/talks/wat

Once you fix that, you'll also need to update your insert statement, which is currently missing the bind variables. See this post (part of a series on REST APIs) for some pointers: https://jsao.io/2018/04/creating-a-rest-api-handling-post-put-and-delete-requests/

Dan McGhan
  • 4,479
  • 1
  • 11
  • 15