0

Sup!

So let's start by putting my code and the error I have.

There is my code :

login(token = 'string') {
         if (!token) throw "Veuillez entrer un token"
         request(
            {
                url : this.baseURL+this.endpoints[2],
                headers : {
                    "auth" : token
                }
            },
            function (err, response, body) {
                // Do more stuff with 'body' here
                if (err) throw err
                if (body.status == 401) throw "Le token "+token+" est invalide."
                this.token = token
                return "Logged in with "+token+" as "+body.username
            }
        );
     }

Here is the code I use to test my client:

const c = require('./client/clienttest')
const client = new c()

let t=client.login("APITESTKORO")
console.log(t)

And finally, the thing that logs in the console. It's not that hard, it's just undefined...

And that's why I need help. I would like to log Logged in with (token) as (username) instead.

I hope somebody can help me !

koro
  • 111
  • 1
  • 13
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – eol Mar 19 '20 at 18:45
  • @eol I don't really understand, I have to make that function asynchronous? – koro Mar 19 '20 at 18:47
  • Beacause I don't want to make it asynchronous ^^" – koro Mar 19 '20 at 18:48

3 Answers3

2

Try using :

login(token = 'string',callback) {
         if (!token) throw "Veuillez entrer un token"
         request(
            {
                url : this.baseURL+this.endpoints[2],
                headers : {
                    "auth" : token
                }
            },
            function (err, response, body) {
                // Do more stuff with 'body' here
                if (err) throw err
                if (body.status == 401) throw "Le token "+token+" est invalide."
                this.token = token
                return callback("Logged in with "+token+" as "+body.username);
            }
        );
     }

Here is the code to test my client:

const c = require('./client/clienttest')
const client = new c()
client.login("APITESTKORO",function(response){
    // Here you have access to your variable
    console.log(response);
})

Hope it helps.

AyushKatiyar
  • 1,000
  • 8
  • 15
1

You can try yield also, Try using:

login(token = 'string') {
         if (!token) throw "Veuillez entrer un token"
         request(
            {
                url : this.baseURL+this.endpoints[2],
                headers : {
                    "auth" : token
                }
            },
            function (err, response, body) {
                // Do more stuff with 'body' here
                if (err) throw err
                if (body.status == 401) throw "Le token "+token+" est invalide."
                this.token = token
                return "Logged in with "+token+" as "+body.username
            }
        );
     }

And, for the client testing:

const c = require('./client/clienttest')
const client = new c()

let t= yield client.login("APITESTKORO")
console.log(t)
AyushKatiyar
  • 1,000
  • 8
  • 15
0

Finnaly, my friend Ness found! Thanks to him!

So, I wanted to not make it asynchronous, to make it easier than making functions to log in, execute them, etc.

Here is the code he sent me!

login(token = 'string') {
         if (!token) throw "Veuillez entrer un token"
         return new Promise ((resolve, reject) => {
          request(
            {
                url : this.baseURL+this.endpoints[2],
                headers : {
                    "auth" : token
                }
            },
            function (err, response, body) {
                // Do more stuff with 'body' here
                if (err) throw err
                if (body.status == 401) throw "Le token "+token+" est invalide."
                this.token = token
                resolve("Logged in with "+token+" as "+body.username)
            }
        );
        })
     }

And, for the client testing:

let c = require('./client/clienttest'),
client = new c();

client.login("APITESTKORO").then(result => console.log(result))

Thanks another time bro!

koro
  • 111
  • 1
  • 13