-1

I have a problem with this piece of code:

async getDomains (req, res) {
    try {
        let domains = await Domain.findAll({ raw: true })

        for(domain of domains) {
            console.log('1')
            var options = {
                host: domain.name,
                port: 443,
                method: 'GET'
            };

            var request = https.request(options, (res) => {
                console.log('2')
                console.log('iam here')
                domain.ssl = {
                    'valid_until': res.connection.getPeerCertificate().valid_from
                }
            });

            console.log('3')
            request.end();
        }

        console.log('4')

        res.send(domains)
    } catch(err) {
        res.status(400).send({
            error: err
        })
    }
},

The output should be 1, 2, 3, 4 but instead I got 1, 3, 4, 2.

Does anyone have an idea how to achieve that?

Delaey
  • 13
  • 3
  • 1
    this is because by the time https.request process the request the parser has already executed log 3 and 4. – rags2riches-prog Dec 09 '20 at 18:10
  • 1
    You should warp the request with a Promise and await it. See: https://stackoverflow.com/questions/52951091/how-to-use-async-await-with-https-post-request – Or Assayag Dec 09 '20 at 18:11
  • 1
    Does this answer your question? [how to use async await with https post request](https://stackoverflow.com/questions/52951091/how-to-use-async-await-with-https-post-request) – eol Dec 09 '20 at 18:15
  • If you can't `await https.request()`, use a library like `node-fetch` so you can `await fetch()`. Will save you a few headaches – Jeremy Thille Dec 09 '20 at 18:20
  • @Assayag i did that but it getting stuck inside the request and everything after that wont be executed. – Delaey Dec 09 '20 at 18:53
  • @JeremyThille Thanks for the Tip i will try that! but first i want to learn the way to do it without using a library. – Delaey Dec 09 '20 at 18:54

1 Answers1

1

You've provided a callback to request, so it's going to send the request and move on, only logging 2 once a response is received. You'll want to use some sort of Promise or async/await to wait for the response from your request.

var request = await (new Promise((resolve, reject) => {
  https.request(options, (res) => {
    console.log('2');
    console.log('i am here');
    domain.ssl = { /* stuff */ };
    resolve();
  });
));
  • this is correct, but there is no need to wrap `new Promise` in parenthesis since you aren't chaining anything onto the created Promise object. – r3wt Dec 09 '20 at 18:18
  • Thank you very much! I have a other Problem now: Its like its not going out of the request and printing for example ''3" or "4", do you have an idea how? I think its stuck inside the request . – Delaey Dec 09 '20 at 18:43
  • and after some minutes i got "Error: socket hang up" – Delaey Dec 09 '20 at 19:17
  • @Delaey you could add error callbacks for `req` ```javascript req.on('error', (err) => { console.log(err); }); ``` – ginga_ninja217 Dec 09 '20 at 20:09
  • @ginga_ninja217 i solved it by replacing "request" with "get" and it worked like a charm, thank you veru much for zour help! – Delaey Dec 10 '20 at 14:29