Return a variable (Singleton) using Node that depends on Promise

Asked

Viewed 212 times

1

Good afternoon, you guys,

It has to create a variable in Nodejs/JS where I would return a value only to be used several times throughout the program?

Exemplifying my problem:
I have to log on to a platform and then send data to her. When logging in, it returns to me a "key" that with it I can send the data.

I will have to send the data several times and the login will always be the same, based on this, it would be possible to have a variable that will receive from the function that login only the key?

The code that I tried initially did not use files as can be seen below:

var retornaToken = restclient.post(url, args, function (data, res) {
        if(res.statusCode === 200){
            console.log("Conectou com sucesso");
            return data.chave;
        } else {
            console.log("Opa, não conectou");
            return false;
        }
});

module.exports = {
    retornaToken : retornaToken
};

however, in another class when calling backToken, the return I have is the Clientrequest and not the token.

So I started using Promises using the package Q Node, however, I still have trouble.

I already created a function to login using files

function realizaLogin(user, pass) {
    var defer = q.defer();

    var args = {
        headers: {"Content-Type": "application/json"},
        data: {username: user, password: pass}
    };

    restclient.post(url, args, function (data, res) {
        if (res.statusCode !== 200) {
            console.log("Opa, deu ruim");
            defer.reject("Deu ruim!");
        }
        else {
            console.log("Conectou com sucesso");
            defer.resolve(data);
        }
        //console.log("Tudo OK!\n");
        //console.log(data.access_token);    
    });
    return defer.promise; 
}

var login = realizaLogin(user, pass);

module.exports = { login : login };

But when I tried to call login (which is exported) as follows

var login = require('./controller');

var token = login.login; // ou var token = login;

console.log(token);

I get the following return on the console:

{ state: 'pending' }
Conectou com sucesso

I’ve tried using the Q.all to be able to see if it expects to log in, creating another class and transforming the realizaLogin into an array of files but without success as well.

Does anyone know any way to export only the key to avoid having to log in every time I have to send data to the server?

  • why not just store the token in a variable?

  • that’s exactly what I want rsrsrs to do. The problem is waiting for the request to return this variable that will have the token.

  • creates a variable outside the callback, for example in the global scope var token, from there inside the Calback you assign the value p that variable token=data.chave.

  • yes, I’ve done that too, the problem is that the JS does not wait for the end of the function realizaLogin in order to return the correct value, it always ends up returning null, or empty string.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.