Titanium 钛加速计;CommonJS没有方法

Titanium 钛加速计;CommonJS没有方法,titanium,appcelerator,commonjs,Titanium,Appcelerator,Commonjs,我是Appcelerator的新手,我尝试导入自己的commonJS库。我按照上面的指示去做 并创建了一个名为“logger.js”的新文件,其代码如下: exports.info = function(str) { Titanium.API.info(new Date()+': '+str); }; 现在,我只是简单地尝试使用以下代码: var logger = require('logger'); logger.info('TEST TEST TEST'); 就像这个例子一样。他找

我是Appcelerator的新手,我尝试导入自己的commonJS库。我按照上面的指示去做

并创建了一个名为“logger.js”的新文件,其代码如下:

exports.info = function(str) {
  Titanium.API.info(new Date()+': '+str);
};
现在,我只是简单地尝试使用以下代码:

var logger = require('logger');
logger.info('TEST TEST TEST');
就像这个例子一样。他找到了文件,但没有识别我的方法,我得到了以下异常:

[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'
我想这很简单,但我不知道哪里是我的错


提前感谢

您显示的代码对我有用。您是否在app/lib目录中创建了logger.js

也许您应该尝试注释index.js中的logger.info(…)行,以确保您看到的是正确的问题;-)

您使用的是哪个版本的Tianium Studio?-在哪个操作系统上


/John

最好导出主对象和访问信息功能(钛合金良好实践)

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

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

我希望我的回答能帮助你;)

通常我们会将这种类型的额外函数文件放在lib目录下,因此您应该创建一个文件夹,并将其命名为lib,然后将logger.js文件放在该文件夹下,然后重试。

最后我得到了它。问题是我使用的是“合金项目”,就像“亚历杭德罗F.卡雷拉”提到的那样。我只需要使用
Alloy.createController()以使其工作

var logger = Alloy.createController('logger');
logger.info('TEST TEST TEST');
现在它工作了


感谢大家为我指出了正确的方向

Hi,该文件位于app/controllers下。如果我取消对行logger.info(…)的注释,我没有得到任何异常。我使用的是Windows 7、Tianium Studio 3.3.0和Tianium SDK 3.3.0.GA。我添加了这一行
Ti.API.log(“Logger:+Logger”)我可以看到他找到了我的文件,甚至输出了我的方法
…e.info=function(t){Titanium.API.info(new Date+“:“+t)}…
。好的,首先,据我所知,这些类型的CommonJS库应该放在app/lib下。然而,这似乎不是你的问题-只是一个更好的做法;-)我认为你的项目可能有问题。所以试试这个:1。创建一个新的Alloy项目,并预览它(在设备上或emulator/Genymotion中)。2.一旦这一切按预期进行,请将您的库和两行添加到index.js。发生了什么事?。。。还有一个指向关于放置commonjs的另一个问题的指针:。顺便说一句,如果你还没有注册成为Appcelerator开发者,我强烈建议你这么做。嗨,很遗憾我得到了同样的例外:-(.你的项目是Alloy类型而不是Mobile项目,所以你必须阅读这个链接,我也这么做了,但遗憾的是结果是一样的:-(