简体   繁体   中英

Access parameter of Node.js module function

im totally new to node.js and i couldn't find a similar question for my problem. 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! 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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