Search code examples
javascripthttpsails.jsnewrelic

sails.js vague error message. with xmlHttpRequest()


I am querying data from newrelic insights in a backed application using sails.js. my function in common controllers->newrelic->number-on-site.js looks like

  fn: async function (inputs, exits) {

    var client = new XMLHttpRequest();
    client.open("GET", "https://insights-api.newrelic.com/v1/accounts/<acount_number>/query?nrql=SELECT ...", true);
    client.setRequestHeader("Accept", "application/json");
    client.setRequestHeader("X-Query-Key", "<my_key>");
    client.send();
    return exits.success();
  }

note that is request worked as a curl request so the data is correct now sails is throwing the following warning.

warn: Files in the `controllers` directory may be traditional controllers or
action files.  Traditional controllers are dictionaries of actions, with
pascal-cased filenames ending in "Controller" (e.g. MyGreatController.js).
Action files are kebab-cased (e.g. do-stuff.js) and contain a single action.
The following file was ignored for not meeting those criteria:
 warn: - newrelic.ts

What exactly is this tring to say? Googling this error message litterally brings up nothing. it almost seams like I have a naming problem but I used

sails generate action newrelic.numberOnSite

to generate this controller so I'm not sure what is going on.

Edit my route looks like:

 '/newrelic/numberOnSite': 'newrelic.numberOnSite',

in case that helps.


Solution

  • It looks like you are mixing controllers and actions you should read here or read on.

    A traditional controller uses pascal-cased ending in Controller eg. NewrelicController.js and, as stated, is a dictionary of actions (functions).

    An action (function) is kebab-cased eg. api/controllers/newrelic/number-on-site.js.

    Controller

    If you are generating a Controller then don't contain in a folder...

    sails generate controller newrelic numberOnSite
    

    Then in your routes.js you would route your path like this...

    'GET /newrelic/numberOnSite': 'NewrelicController.numberOnSite',
    

    Action

    If you are generating an action then contain these in a folder...

    sails generate action newrelic/numberOnSite
    

    Then in your routes.js you would route your path like this...

    'GET /newrelic/numberOnSite': {action:'newrelic/number-on-site'},
    

    A Controller requires less code when compared to a collection of actions though Actions, introduced in Sails v1.0, is preferred over Controllers as it easier to maintain and debug.