Skip to Content
avatar image
Former Member

XSA - node client = req.db undefined

I am writing an XSA application that uses Node to talk to HANA database without using odata. I am unable to populate the req.db.

Any help will be greatly appreciated!

Here is my node server file:

var xsjs  = require("sap-xsjs");
var xsenv = require("sap-xsenv");
var port  = process.env.PORT || 3000;
var server = require('http').createServer();
var express = require("express");
var node = require("./routes/nodeapp"); 
var passport = require("passport");
var xsHDBConn = require("sap-hdbext");
var xssec = require("sap-xssec");


var app = module.exports = express();
app.use("/api", node());
//configure HANA 
var options = Object.assign({redirectUrl: "/index.xsjs"}, 
	xsenv.getServices({
		uaa: { tag: "xsuaa" },
		hana: { tag: "hana" }
	})
);
passport.use("JWT", new xssec.JWTStrategy(options.uaa));

app.use(
	passport.authenticate("JWT", { session: false }),
	xsHDBConn.middleware(options.hana)
);

var xsjsApp = xsjs(options);
app.use(xsjsApp);
app.listen(port); 


Here is my node route handler file:

var express = require("express");
module.exports = function(){
	var router = express.Router();
	router.route("/")
	  .get(function(req, res) {
	    res.send("Hello World App Node.js");
	});

	router.route("/dummy")
		.get(function(req, res){
			//console.log(req);
		//!!!!this is not getting populated!!
 			var client = req.db;
    		client.prepare(
        		"select SESSION_USER from \"MD.ProductView\" ", 
        		function (err, statement){
            		statement.exec([],
                		function (err1, results) {
                	    	if (err1){
                	        	res.type("text/plain").status(500).send("ERROR: " + err1);
                	    	}else{
                	        	var result = JSON.stringify( { Objects: results });
                	        	res.type("application/json").status(200).send(result);
                	    	}
            	    	}
            		);
        		}
    		);
		});

   return router;
};
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Oct 16, 2017 at 05:35 PM

    I was creating the node route too soon. Creating it after the middle ware fixed the problem.

    Add comment
    10|10000 characters needed characters exceeded