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
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
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 ;)
通常我们习惯把这种类型的额外的函数文件放在lib目录下,所以你应该创建一个文件夹并在应用程序目录下将它命名为lib,并将logger.js文件放在该文件夹下,然后重试。
链接地址: http://www.djcxy.com/p/96612.html上一篇: iOS CommonJS模块返回“未定义不是构造函数”错误
下一篇: 钛加速器; CommonJS没有办法