Murolack
Murolack

Reputation: 2167

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

Upvotes: 0

Views: 818

Answers (4)

Murolack
Murolack

Reputation: 2167

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

Upvotes: 0

Mitul Bhalia
Mitul Bhalia

Reputation: 1217

Normally we used to put this type of extra functions files under lib directory so you should create one folder and named it lib under app directory and put logger.js file under that folder and try again.

Upvotes: 0

Alejandro F. Carrera
Alejandro F. Carrera

Reputation: 403

It is better exports Main Object and access info function (Titanium Good Practices).

logger.js

var logger = (function(){

    var self = {};

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

    return self;

}());

module.exports = logger;

file.js where you need logger

var loggerObject = require('logger.js'); // (both files are at the same Path)
loggerObject.info("TEST TEST");

I hope my answer helps you ;)

Upvotes: 1

John Dalsgaard
John Dalsgaard

Reputation: 2807

The code you showed works for me. Did you create the logger.js in the app/lib directory?

Perhaps you should try to comment out the logger.info(...) line in index.js just to ensure that you are looking at the right problem ;-)

Which version of Titanium Studio are you using? - and on which OS?

/John

Upvotes: 1

Related Questions