0 Replies Latest reply on Jun 10, 2019 8:05 PM by bc_uk

    Can node-oracledb be used in Mobile Hub Custom APIs?

    bc_uk

      From within my Mobile Hub Custom APIs I need to be able to directly access an Oracle DB instance using Basic authentication. As per the Oracle Mobile Platform videos on youtube, I added the oracledb package as a dependency:

       

      {

        "name": "patients",

        "version": "1.0.0",

        "description": "API to handle getting, adding, and updating of patient records.",

        "main": "patients.js",

        "dependencies": {

          "oracledb": "3.1.2"

        },

        "oracleMobile": {

          "dependencies": {

            "apis": {},

            "connectors": {}

          }

        }

      }

       

      I then executed "npm install" within the package folder, and it completed without error, and generated a package-lock.json file:

       

      {

        "name": "patients",

        "version": "1.0.0",

        "lockfileVersion": 1,

        "requires": true,

        "dependencies": {

          "oracledb": {

            "version": "3.1.2",

            "resolved": "https://registry.npmjs.org/oracledb/-/oracledb-3.1.2.tgz",

            "integrity": "sha512-DOBKpUlfvGAX6bcpuPGtPVVfOOUqFue8eLjbnnmkDyBTU+YuH+gXHRy4ftlFzkHBXYkrSGenFJrXy8dDfEVXCg=="

          }

        }

      }

       

      However, when I uploaded the Custom API package and tested it, I got the following error:

       

      MOBILE-43560: Custom Code VM: Failed to start container due to an error in the custom code: /u01/app/oracle/mobile/mobile_ccc/custom_code_modules/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_6/patients/patients.js:29
        connection = await oracledb.getConnection( {
                           ^^^^^^^^

      SyntaxError: Unexpected identifier.

       

      Here is my full node.js code for the Custom API:

       

      module.exports = function(service) {

       

          service.get('/mobile/custom/patients/patients', function(req,res) {

       

              var oracledb = require('oracledb');

       

              let connection;

       

              try {

                  connection = await oracledb.getConnection( {

                      user          : "system",

                      password      : "xxxxxxxxxx",

                      connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xx.xx.xxx)(PORT = 1521))(CONNECT_DATA =(SID= db1)))"

                  });

       

                  let result = await connection.execute('SELECT * FROM patients');

                  console.info(result.rows);

                

              } catch (err) {

                  console.error(err);

              } finally {

                  if (connection) {

                    try {

                      await connection.close();

                    } catch (err) {

                      console.error(err);

                    }

                  }

              }

        

              var result = {};

              var statusCode = 200;

              res.status(statusCode).send(result);

          });

       

          // router: get patient by id

          service.get('/mobile/custom/patients/patients/:id', function(req,res) {

              var result = {};

              var statusCode = 200;

              if (statusCode == 200){

                  var acceptType = req.accepts(['application/json']);

                  if (acceptType == 'application/json'){

                      result = {

                            "items":

                              [

                                {

                                  "id": 1,

                                  "first_name": "Ted",

                                  "surname": "Smith",

                                  "gender": "M",

                                  "dob": "12-Jun-1987"

                                }

                              ]

                          };

                  }

              }

              res.status(statusCode).send(result);

          });

       

      };

       

      Please advise.