Search code examples
titaniumappceleratorcommonjs

Titanium Appcelerator; CommonJS has no method


I'm pretty new to Appcelerator and I tried to import my own commonJS Library. I followed the instructions on

http://docs.appcelerator.com/titanium/latest/#!/guide/CommonJS_Modules_in_Titanium

and created a new file named "logger.js", with the following code:

exports.info = function(str) {
  Titanium.API.info(new Date()+': '+str);
};

Now I simply try so exceute this code with:

var logger = require('logger');
logger.info('TEST TEST TEST');

Just like in the example. He found the the file, but didn't recognize my method and I get the following exception:

[ERROR] :  TiExceptionHandler: (main) [602,602] ----- Titanium Javascript Runtime Error -----
[ERROR] :  TiExceptionHandler: (main) [0,602] - In alloy/controllers/index.js:100,12
[ERROR] :  TiExceptionHandler: (main) [0,602] - Message: Uncaught TypeError: Object function Controller() {
[ERROR] :  TiExceptionHandler:     function logOutput(str) {
[ERROR] :  TiExceptionHandler:         Titanium.API.info(str);
[ERROR] :  TiExceptionHandler:     }
[ERROR] :  TiExceptionHandler:     require("alloy/controllers/BaseController").apply(this, Array.prototype.slice.call(arguments));
[ERROR] :  TiExceptionHandler:     this.__controllerPath = "login";
[ERROR] :  TiExceptionHandler:     if (arguments[0]) {
[ERROR] :  TiExceptionHandler:         __processArg(arguments[0], "__parentSymbol");
[ERROR] :  TiExceptionHandler:         __processArg(arguments[0], "$model");
[ERROR] :  TiExceptionHandler:         __processArg(arguments[0], "__itemTemplate");
[ERROR] :  TiExceptionHandler:     }
[ERROR] :  TiExceptionHandler:     var $ = this;
[ERROR] :  TiExceptionHandler:     var exports = {};
[ERROR] :  TiExceptionHandler:     exports.destroy = function() {};
[ERROR] :  TiExceptionHandler:     _.extend($, $.__views);
[ERROR] :  TiExceptionHandler:     exports = logOutput;
[ERROR] :  TiExceptionHandler:     _.extend($, exports);
[ERROR] :  TiExceptionHandler: } has no method 'info'
[ERROR] :  TiExceptionHandler: (main) [1,603] - Source:     logger.info("TEST TEST TEST");
[ERROR] :  V8Exception: Exception occurred at alloy/controllers/index.js:100: Uncaught TypeError: Object function Controller() {
[ERROR] :  V8Exception:     function logOutput(str) {
[ERROR] :  V8Exception:         Titanium.API.info(str);
[ERROR] :  V8Exception:     }
[ERROR] :  V8Exception:     require("alloy/controllers/BaseController").apply(this, Array.prototype.slice.call(arguments));
[ERROR] :  V8Exception:     this.__controllerPath = "login";
[ERROR] :  V8Exception:     if (arguments[0]) {
[ERROR] :  V8Exception:         __processArg(arguments[0], "__parentSymbol");
[ERROR] :  V8Exception:         __processArg(arguments[0], "$model");
[ERROR] :  V8Exception:         __processArg(arguments[0], "__itemTemplate");
[ERROR] :  V8Exception:     }
[ERROR] :  V8Exception:     var $ = this;
[ERROR] :  V8Exception:     var exports = {};
[ERROR] :  V8Exception:     exports.destroy = function() {};
[ERROR] :  V8Exception:     _.extend($, $.__views);
[ERROR] :  V8Exception:     exports = logOutput;
[ERROR] :  V8Exception:     _.extend($, exports);
[ERROR] :  V8Exception: } has no method 'info'

I guess it's so simple but I don't where is my fault.

thanks in advance


Solution

  • Finaly I got it. The problems were I'm using an "Alloy Project", just like "Alejandro F. Carrera" mentioned it. I simply have to use Alloy.createController(); to get it work.

    var logger = Alloy.createController('logger');
    logger.info('TEST TEST TEST');
    

    now it work.

    Thanks to all of you to pointing me to the right direction