Canot read property data of... /QOpenSys/QIBM/ProdData/OPS/Node6/os400/xstoolkit/lib/istoredp.js:128:17

Issue #19 new
Dave Heffernan created an issue

All,

I’m working through tutorial outlined here → https://developer.ibm.com/articles/i-native-js-app-ibmi-with-nodejs/

CALL QP2TERM

set PATH='/QOpenSys/QIBM/ProdData/OPS/Node6/bin:/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin'

node -v
v6.14.4
\$

Here is the resultant tutorial code

var http = require('http');
var fs = require('fs'); 
var url = require('url'); 
var db = require('/QOpenSys/QIBM/ProdData/OPS/Node6/os400/db2i/lib/db2a');
var xt = require('/QOpenSys/QIBM/ProdData/OPS/Node6/os400/xstoolkit/lib/itoolkit');

var DBname = "MTCP8PRD"; 
var userId = "djhxxxx";
var passwd = "djhxxxxxxx";
var ip = "10.65.128.1"; 
var port = 9876; 

var webserver = http.createServer((req,res) => { 
  var realPath = __dirname + url.parse(req.url).pathname; 
  fs.exists(realPath, (exists) => { 
    if(!exists){ 
      var sql = url.parse(req.url, true).query.sql;
      var cl = url.parse(req.url, true).query.cl;
      if(sql && sql.length > 0) {
        console.log("SQL statement : " + sql);
        var dbconn = new db.dbconn();
        dbconn.conn(DBname, userId, passwd);  // Connect to the DB 
        var stmt = new db.dbstmt(dbconn);
        stmt.exec(sql, (rs) => { // Query the statement
          res.writeHead(200, {'Content-Type': 'text/plain'});
          res.end(JSON.stringify(rs));
          stmt.close();  
          dbconn.disconn(); 
          dbconn.close(); 
        });
      }
      if(cl && cl.length > 0) {
        console.log("CL statement : " + cl); 
        var conn = new xt.iConn(DBname, userId, passwd);
        conn.add(xt.iSh("system -i " + cl)); 
        conn.run((rs) => {
          res.writeHead(200, {'Content-Type': 'text/plain'});
          res.end(xt.xmlToJson(rs)[0].data);
        }); 
      } 
    } else { 
      var file = fs.createReadStream(realPath); 
      res.writeHead(200, {'Content-Type':'text/html'}); 
      file.on('data', res.write.bind(res)); 
      file.on('close', res.end.bind(res));  
      file.on('error', (err) => { 
        res.writeHead(500, {'Content-Type':'text/plain'}); 
        res.end("500 Internal Server Error"); 
      }); 
    }  
  }); 
}); 
webserver.listen(port, ip); 
console.log('Server running at http://' + ip + ':' + port);

Here is a screen-shot of the resultant page

DB Select statements work just fine

When I opt to run a command - any command - I receive the following

 > node /home/djh0804/sample5.js                                                     
   Server running at http://10.65.128.1:9876                                         
   CL statement : WRKSYSSTS                                                          
   /home/djh0804/sample5.js:38                                                       
             res.end(xt.xmlToJson(rs)[0].data);                                      
                                        ^                                            

   TypeError: Cannot read property 'data' of undefined                               
       at conn.run (/home/djh0804/sample5.js:38:38)                                  
       at /QOpenSys/QIBM/ProdData/OPS/Node6/os400/xstoolkit/lib/istoredp.js:128:17   
   $                                                                                 

Is this a bug? Do I have something set up incorrectly?

Dave H.

Comments (0)

  1. Log in to comment