简体   繁体   English

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

[英]Access parameter of Node.js module function

im totally new to node.js and i couldn't find a similar question for my problem. 我对node.js完全陌生,我找不到与我的问题类似的问题。 I'm sure it's easy to solve for one of u guys... at least i guess. 我敢肯定,解决你们中的一个很容易...至少我猜。

I'm trying to get a special paragraph of a wikipage using the npm mediawiki module for node.js! 我正在尝试使用用于node.js的npm mediawiki模块获取维基页面的特殊段落! I get the paragraph using the pre-defined function as following: 我使用以下预定义函数获取该段落:

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

Thats working. 就是这样。 What i want is: to use "result" outside of that code block in other functions. 我想要的是:在其他功能的代码块之外使用“结果”。 I think it has something to do with callbacks but im not sure how to handle it as this is a pre-defined function from the mediawiki module. 我认为它与回调有关,但是我不确定如何处理它,因为这是来自mediawiki模块的预定义函数。

The example function of the module to get a wikipage looks as following: 获取维基页面的模块的示例功能如下:

/**
     * 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);
};

which uses the following function of the module: 该模块使用以下功能:

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;
}

There's also a complete callback function and i dont know how to use it: 还有一个完整的回调函数,我不知道如何使用它:

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

How can i access the "result" variable by using callbacks outside the function of the module? 如何通过使用模块功能之外的回调来访问“结果”变量? I don't know how to handle the callback as it is a pre-defined function of a module... 我不知道如何处理回调,因为它是模块的预定义功能...

What i want is: to use "result" outside of that code block in other functions. 我想要的是:在其他功能的代码块之外使用“结果”。

You can't. 你不能 You need to use the result inside that code block (that code block is called a callback function btw.). 您需要在该代码块内部使用该结果(该代码块称为callback函数btw。)。 You can still pass them to other functions, you just need to do it inside that callback function: 您仍然可以将它们传递给其他函数,只需要在该回调函数中执行即可:

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