繁体   English   中英

Node.js模块功能的访问参数

[英]Access parameter of Node.js module function

我对node.js完全陌生,我找不到与我的问题类似的问题。 我敢肯定,解决你们中的一个很容易...至少我猜。

我正在尝试使用用于node.js的npm mediawiki模块获取维基页面的特殊段落! 我使用以下预定义函数获取该段落:

bot.page(title).complete(function (title, text, date) {
    //extract section '== Check ==' from wikipage&clean string
    var result = S(text).between('== Check ==', '==').s;
});

就是这样。 我想要的是:在其他功能的代码块之外使用“结果”。 我认为它与回调有关,但是我不确定如何处理它,因为这是来自mediawiki模块的预定义函数。

获取维基页面的模块的示例功能如下:

/**
     * Request the content of page by title
     * @param title the title of the page
     * @param isPriority (optional) should the request be added to the top of the request queue (defualt: false)
     */
    Bot.prototype.page = function (title, isPriority) {
        return _page.call(this, { titles: title }, isPriority);
};

该模块使用以下功能:

function _page(query, isPriority) {
    var promise = new Promise();

    query.action = "query";
    query.prop = "revisions";
    query.rvprop = "timestamp|content";

    this.get(query, isPriority).complete(function (data) {
        var pages = Object.getOwnPropertyNames(data.query.pages);
        var _this = this;
        pages.forEach(function (id) {
            var page = data.query.pages[id];
            promise._onComplete.call(_this, page.title, page.revisions[0]["*"], new Date(page.revisions[0].timestamp));
        });
    }).error(function (err) {
        promise._onError.call(this, err);
    });

    return promise;
}

还有一个完整的回调函数,我不知道如何使用它:

/**
     * Sets the complete callback
     * @param callback a Function to call on complete
     */
    Promise.prototype.complete = function(callback){
        this._onComplete = callback;
        return this;
    };

如何通过使用模块功能之外的回调来访问“结果”变量? 我不知道如何处理回调,因为它是模块的预定义功能...

我想要的是:在其他功能的代码块之外使用“结果”。

你不能 您需要在该代码块内部使用该结果(该代码块称为callback函数btw。)。 您仍然可以将它们传递给其他函数,只需要在该回调函数中执行即可:

bot.page(title).complete(function (title, text, date) {
    //extract section '== Check ==' from wikipage&clean string
    var result = S(text).between('== Check ==', '==').s;

    other_function(result); // <------------- this is how you use it
});

暂无
暂无

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

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