未定义的不是功能 - 流星智能包

时间:2014-08-31 19:32:59

标签: javascript meteor undefined

最近,我一直在尝试为我正在进行的项目开发一个智能包,我遇到了问题。

以下代码位于server/methods.js内:

Meteor.startup(function() {

    Meteor.methods({

        // ISBN Lookup for AWS
        isbnAWS: function(isbn) {

            OperationHelper = apac.OperationHelper;

            isbn = isbn.replace(/-/g,"");
            console.log("Looking up ISBN: " + isbn);
            cachedBook = BookCache.findOne({"isbn": isbn});

            // If the book isn't in cache, use apac to get information into cache.
            if (!cachedBook) {

                // Instantiate the OH object for APAC.
                OpHelper = new OperationHelper({
                    awsId: AWS_KEY_ID,
                    awsSecret: AWS_SECRET_KEY,
                    assocId: AWS_ASSOC_ID
                });

                // Use OH to query AWS, synchronously.
                result = OpHelper.executeSync({
                    SearchIndex: 'Books',
                    ResponseGroup: 'Medium,Images',
                    IdType: 'ISBN',
                    ItemId: isbn
                });

                console.log(result);

            } else {
                console.log("Using cache for ISBN: " + isbn);
            }

        }

    });

});

此方法由项目模板中的事件处理程序调用,如下所示:

"click #isbn-btn": function() {

    // Store the current ISBN in Session, for reactivity.
    theISBN = $("#isbn").val().trim().replace(/-/g, "");
    Session.set("aws-isbn", theISBN);

    // Make a call to our Meteor.method, which will contact AWS, and give us
    // a result.
    Meteor.call("isbnAWS", theISBN, function(err, res) {
        if (err)
            console.log("Error: " + err);
        else
            console.log("Success!");
    });

}

OperationHelper是我编写的包的一部分,它包含此代码,位于包lib/meteor-apac.js内:

if (!Meteor.isClient) {

    apac = Npm.require("apac-czbaker");
    OperationHelper = apac.OperationHelper;

    function makeSyncMethod(method){
        var wrapped=Meteor._wrapAsync(method);
        var syncMethod=function(){
            return wrapped.apply(this,arguments);
        };
        return syncMethod;
    }

    OperationHelper.prototype.executeSync = makeSyncMethod(OperationHelper.prototype.execute);

}

这是我的package.js,其中apac对象已明确导出以供我的项目使用:

Package.describe({
  summary: "Access to the Amazon Product Advertising API, using the NodeJS 'apac' module.",
  version: "0.0.4",
  git: "https://github.com/czbaker/meteor-apac.git",
  name: "czbaker:apac"
});

Npm.depends({
  "apac-czbaker": "1.0.0"
});

Package.onUse(function(api) {
  api.versionsFrom('METEOR@0.9.0.1');
  api.use('underscore', 'server');
  api.addFiles('lib/meteor-apac.js');
  api.export('apac');
});

出于某种原因,当我尝试使用我尝试使用我的包添加的executeSync()函数时,我得到以下输出:

I20140831-15:16:56.651(-4)? Looking up ISBN: 
W20140831-15:16:56.826(-4)? (STDERR) 
W20140831-15:16:56.826(-4)? (STDERR) events.js:72
W20140831-15:16:56.827(-4)? (STDERR)         throw er; // Unhandled 'error' event
W20140831-15:16:56.827(-4)? (STDERR)               ^
W20140831-15:16:56.831(-4)? (STDERR) TypeError: undefined is not a function
W20140831-15:16:56.831(-4)? (STDERR)     at /home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/lib/OperationHelper.js:85:17
W20140831-15:16:56.832(-4)? (STDERR)     at Parser.<anonymous> (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/lib/xml2js.js:384:20)
W20140831-15:16:56.832(-4)? (STDERR)     at Parser.emit (events.js:95:17)
W20140831-15:16:56.832(-4)? (STDERR)     at Object.onclosetag (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/lib/xml2js.js:348:26)
W20140831-15:16:56.832(-4)? (STDERR)     at emit (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/node_modules/sax/lib/sax.js:615:33)
W20140831-15:16:56.833(-4)? (STDERR)     at emitNode (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/node_modules/sax/lib/sax.js:620:3)
W20140831-15:16:56.833(-4)? (STDERR)     at closeTag (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/node_modules/sax/lib/sax.js:861:5)
W20140831-15:16:56.834(-4)? (STDERR)     at Object.write (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/node_modules/sax/lib/sax.js:1294:29)
W20140831-15:16:56.834(-4)? (STDERR)     at Parser.exports.Parser.Parser.parseString (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/lib/xml2js.js:403:31)
W20140831-15:16:56.834(-4)? (STDERR)     at Parser.parseString (/home/misutowolf/projects/meteor-apac/.build.czbaker:apac/npm/node_modules/apac-czbaker/node_modules/xml2js/lib/xml2js.js:6:61)
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.

从我收集的内容来看,似乎它试图说executeSync不是一个函数,但是......显然是这样。我很困惑。我不知道我在这里做错了什么。任何想法都将不胜感激。

1 个答案:

答案 0 :(得分:1)

  1. OperationHelper.prototype.execute需要3个参数function(operation, params, callback)
  2. Meteor._wrapAsync只是附加一个额外的参数作为回调
  3. 您只向OpHelper.executeSync
  4. 提供1个参数

    最终结果是&#34;自动&#34; Meteor._wrapAsync提供的回调作为第二个参数params传递而不是作为第三个参数&#34;回调&#34;。

    更改您的server/method.js以向该函数提供空params,您应该没问题。例如:

                result = OpHelper.executeSync({
                    SearchIndex: 'Books',
                    ResponseGroup: 'Medium,Images',
                    IdType: 'ISBN',
                    ItemId: isbn
                }, {});