apigee-127 / a127-documentation

Documentation for Apigee-127
113 stars 24 forks source link

config/xx.yaml not loading, key/variable names returns undefined? #16

Closed hayderma closed 6 years ago

hayderma commented 6 years ago

I am trying to load environment configuration files, but neither mydefault.yaml nor my newly added dev.yaml are being loaded, I am using webstorm and added environment variable NODE_ENV=dev , I also tried it from commandline node app.js NODE_ENV=dev , also a127 project start NODE_ENV=dev , also tried a127 project start dev. Nothing is working, the code req.config.varName and req.configare both returning undefined from inside the controllers , I tried switching NODE_ENV off (should load default.yaml by default) but it is also returning undefined. below is my app.js

'use strict';

var a127 = require('a127-magic');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(express.bodyParser());
module.exports = app; // for testing

// initialize a127 framework
a127.init(function(config) {

  // include a127 middleware
  app.use(a127.middleware(config));
  var magic = config['a127.magic'];
  config.basePath = magic.swaggerObject.basePath;

  config.ui ={
    swaggerUi: config.basePath + "/ui",
    apiDocs: config.basePath + "/spec"
  };

  app.use(config['a127.magic'].swaggerTools.swaggerUi(config.ui));

  // error handler to emit errors as a json string
  app.use(function(err, req, res, next) {
    if (typeof err !== 'object') {
      // If the object is not an Error, create a representation that appears to be
      err = {
        message: String(err) // Coerce to string
      };
    } else {
      // Ensure that err.message is enumerable (It is not by default)
      Object.defineProperty(err, 'message', { enumerable: true });
    }

    // Return a JSON representation of #/definitions/ErrorResponse
    res.set('Content-Type', 'application/json');
    res.end(JSON.stringify(err));
  });

  var ip = process.env.IP || 'localhost';
  var port = process.env.PORT || 10010;
  // begin listening for client requests
  app.listen(port, ip);

  var urlBasePath = magic.swaggerObject.schemes[0] + '://localhost:' + port;
  console.log('API ui:' + urlBasePath + config.ui.swaggerUi);
  console.log('API spec:' + urlBasePath + config.ui.apiDocs);

});

This is the debugger console, I can clearly see that the configs are there but too deep, and they return undefined when i tried accessing them.

debug

hayderma commented 6 years ago

For those who may come across this issue in the future, after further debugging I found : The configuration can be accessed with req.a127.config('attributeName') , not req.config, also it turned out that config is a function, not a JSON object, thus must pass key name to it.