简体   繁体   English

未定义不是功能-流星智能套餐

[英]Undefined not a function — Meteor Smart Package

Recently, I've been trying to work on a smart package for a project I'm working on, and I'm having a problem. 最近,我一直在尝试为我正在进行的项目开发智能软件包,但遇到了问题。

The following code lives inside server/methods.js : 以下代码位于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);
            }

        }

    });

});

This method is being called by an event handler in a project template, as such: 项目模板中的事件处理程序将调用此方法,如下所示:

"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 is part of the package I have written, which contains this code, inside the package's lib/meteor-apac.js : 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);

}

And this is my package.js , where the apac object is clearly exported for use in my project: 这是我的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');
});

For some reason, I get the following output when I try to use the executeSync() function that I attempt to add using my package: 出于某种原因,当我尝试使用通过包尝试添加的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.

From what I gather, it seems like it's trying to say that executeSync isn't a function, but...it clearly is. 据我所知,似乎是在试图说executeSync不是一个函数,但是...显然是。 I'm very confused. 我很困惑 I have no idea what I'm doing wrong here. 我不知道我在做什么错。 Any thoughts would be greatly appreciated. 任何想法将不胜感激。

  1. OperationHelper.prototype.execute takes 3 arguments function(operation, params, callback) OperationHelper.prototype.execute需要3个参数function(operation, params, callback)
  2. Meteor._wrapAsync just tacks on an extra argument as a callback Meteor._wrapAsync只是增加了一个参数作为回调
  3. You only provide 1 argument to OpHelper.executeSync 您仅向OpHelper.executeSync提供1个参数

The net result is the "automatic" callback provided by Meteor._wrapAsync is being passed as the second paarameter params rather than as the 3rd parameter "callback". 最终结果是Meteor._wrapAsync提供的“自动”回调Meteor._wrapAsync被作为第二个params而不是作为第三个参数“回调”传递。

Change your server/method.js to provide an empty params to the function, and you should be fine. 更改server/method.js ,以为该函数提供一个空的params ,您应该没事。 eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM