我目前正在制作一个应用程序,它尝试查找特定域的子域并将其放入数据库中。目前它从crt.sh和threatcrowd获取信息。
另一个函数解析这些网站的HTML并返回一个子域数组,如下所示:
[test.com,cdn.test.com,stream.test.com]
这样可行。
但是下面的功能,它会引起一些问题: 使用下面的函数将数据正确地插入到数据库中,但是promise永远不会解析(“值”永远不会被打印)。
我一直收到这个错误:
dp: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
这是我尝试执行应用程序时得到的输出:
[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 }
[...]
一切正常,如果我只是console.log它。但是,一旦我试图将其付诸实施,我就会遇到问题。
这是我解析cname并将其推送到数据库的函数。
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);
})
};
答案 0 :(得分:2)
Array.forEach
返回undefined。如果您只使用map
:
var dp = domains.map(function(domain) {
return dns.resolveCname (...)
});
return Promise.all(dp)
(...)