A
A
allaga2020-06-09 10:17:25
Node.js
allaga, 2020-06-09 10:17:25

How to call a variable from a custom module in Node.js?

Good afternoon!

foo.js

const needle = require('needle');

function fnCallBack(error, response) {
    module.exports.cookie = response.cookies;
}

function getCookie(fn=fnCallBack) {
    let url = 'http://metodkabi.net.ru/index.php?id=mod&op=user&ts=glavn';

    let identity = 'id_tek=glavn&username=aysien&password=aysien';

    let options = {
        headers: {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.5',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Host': 'metodkabi.net.ru',
            'Origin': 'http://metodkabi.net.ru',
            'Referer': 'http://metodkabi.net.ru/',
            'User-Agent': 'Mozilla/5.0 (Android 9; Mobile; rv:76.0) Gecko/76.0 Firefox/76.0'
        }
    };

    needle.post(url, identity, options, fn);
}

getCookie();


bar.js
const get = require("./foo");

console.log(get.cookie);


Please tell me why when calling console.log(get.cookie); all the time it comes out undefined? How can I fix it?

Thanks for answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-06-09
@allaga

you call console.log before fnCallBack is executed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question