DevotedFish
DevotedFish

Reputation: 3

Promise seems to never resolve

I am currently making an application, that tries to find subdomains for a specific domain and puts it in a database. Currently it gets information from crt.sh and threatcrowd.

Another function parses the HTML of those sites and returns an array of subdomains, like so:

[test.com, cdn.test.com, stream.test.com]

This works as it should.

But the function below, it causes me some issues: The data gets inserted correctly in the DB, using the function below, but promise never resolves (the "value" is never printed).

I keep getting this error:

dp: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined

This is the output I get when I try to execute the application:

[nodemon] 1.17.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
Listening on port 3000
**dp: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined** (<- I think this might be my issue)
domain: order.pizzahut.com
address: www.pizzahut.com.edgekey.net
domain: mobile.pizzahut.com
address: wildcard.pizzahut.com.edgekey.net
[...]
inserted: { names: 'order.pizzahut.com',
  cname: 'www.pizzahut.com.edgekey.net',
  date: 2018-03-15T18:16:49.845Z,
  _id: 5aaab8916967b70abff48280,
  __v: 0 }
inserted: { names: 'social.pizzahut.com',
  cname: 'www.pizzahut.com',
  date: 2018-03-15T18:16:49.861Z,
  _id: 5aaab8916967b70abff48282,
  __v: 0 }
inserted: { names: 'mobile.pizzahut.com',
  cname: 'wildcard.pizzahut.com.edgekey.net',
  date: 2018-03-15T18:16:49.858Z,
  _id: 5aaab8916967b70abff48281,
  __v: 0 }
[...]

It all works fine, if I just console.log it. But as soon as I try to put it into a promise, I run into problems.

This is my function that resolves the cname and pushes it to the database.

funk.lookupDomain = function(post) {
    const crt = funk.crt(post);
    const threatcrowd = funk.threatcrowd(post);

    //Wait for crt & Threatcrowd to return a list of subdomains.
    Promise.all([
        crt,    
        threatcrowd
    ]).then(function(values) {
            //Concat the two lists and making sure there are no duplicates. 
            let domains = arrayUnique(values[0].concat(values[1]));
            //Iterate over each domain.
            var dp = domains.forEach(function (domain) {
                return new Promise(function(resolve, reject) {
                    // Resolve CNames
                    dns.resolveCname(domain, function (error, address) {
                        //Check if the domain resolves to a valid address
                        if (!error) {
                            //If no error, just write names and cnames to the DB.
                            CC.create({names: domain, cname: address}, function (error, insertedDomain) {
                                if (error) {
                                    console.log(error)
                                    reject(error)
                                } else {
                                    console.log(insertedDomain);
                                    resolve(insertedDomain);
                                };

                            });
                        }
                    });

                })
            })
            return Promise.all(dp)
            .then(function(value) {
                console.log(value);
            })
            .catch(function(error) {
                console.log("dp: " + error);
            });
        })
        .catch(function(error) {
            console.log("LOOKUP DOMAIN FAILED: " + error);
        })
};

Upvotes: 0

Views: 349

Answers (1)

Oscar Paz
Oscar Paz

Reputation: 18292

Array.forEach returns undefined. You can get an array of promises if you just use map:

var dp = domains.map(function(domain) {
    return dns.resolveCname (...)
});
return Promise.all(dp)
(...)

Upvotes: 2

Related Questions