Node-oracledb: nodejs to Oracle error

Created on 6 Apr 2020  路  7Comments  路  Source: oracle/node-oracledb

hi ,
i am new to nodejs and trying to build one simple get request connecting to Oracle . I am getting the errors as below.Oracle version is 12, i installed the nodejs and client properly and check few examples from github as well . there is no connectivity issue. i am pasting the code.

//for connection i am using db_properties.js   
module.exports = {
    user: "CLGINFO",
    password: "12345",
    connectString:"194.36.30.13/PROD"
    };
//for fetch i am using the following code
const oracledb = require('oracledb');
const dbConfig = require("../db_properties");

let sql,binds,options,result;
connection = oracledb.getConnection(dbConfig);
let fetch = require("express").Router().get("/",(req,res)=>{
       if(err) throw err;
       else {res.send(records)

    }
});
module.exports = fetch;

//for server.js
let express = require("express");
let app = express();
let bodyparser = require("body-parser");
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:false}));
let cors = require("cors");
app.use(cors());
app.use("/fetch",require("./fetch/fetch"));


app.listen(8080,'127.0.0.1');

console.log("node server listening the port no.8080");

when i run the command node server

node server listening the port no.8080

ReferenceError: err is not defined
    at C:\Users\m.arif\Desktop\DBN\fetch\fetch.js:9:8
    at Layer.handle [as handle_request] (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:47:12)
question

All 7 comments

Hi there,

This has nothing to do with oracle, the error is on your code when you reference err without having it in the callback for express:

require("express").Router().get("/",(req,res)=>{
if(err) throw err;  // there is no err object in the callback, therefore... it throws error.

edit: you might want to remove the actual connection info from your post :) and take note the backtick formatting requires 3 backticks, you used just two.

Good luck :)

Thanks Danilo,

Do you have any sample of fetch api, i tried removing the err and running the script directly.But still there are no records.

ReferenceError: records is not defined
    at C:\Users\m.arif\Desktop\DBN\fetch\fetch.js:9:16
    at Layer.handle [as handle_request] (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\m.arif\Desktop\DBN\node_modules\express\lib\router\index.js:47:12)
const oracledb = require('oracledb');
const dbConfig = require("../db_properties");

let sql,binds,options,result;
connection = oracledb.getConnection(dbConfig);
let fetch = require("express").Router().get("/",(req,res)=>{


      res.send(records)


});
module.exports = fetch;

@ArifMohammed17 did you get a chance to work through https://jsao.io/2018/03/creating-a-rest-api-with-node-js-and-oracle-database/ ? I included this think in my email a few nights ago.

PS I edited your posts to use the backticks like @danilohgds mentioned. Take a look.

Thanks cjbj, i will format it next time.I am confused on when to use bind variables. As my data is coming from angular. do u have any simple insert request or post request sample for my checking.
i checked the referred document. what is the difference between router and contoller.

@ArifMohammed17 I've said in email and the other forum you posted on: use bind variables whenever you can. If you post code snippets, we can help - once you've read the manual, which has a lot of examples, and reviewed the examples directory.

Questions about Express are best asked on an Express forum - or Google !

Thanks.

Closing - no activity.

Was this page helpful?
0 / 5 - 0 ratings