【发布时间】:2018-08-24 16:33:04
【问题描述】:
我目前正在制作一个应用程序,它试图为特定域查找子域并将其放入数据库中。目前它从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);
})
};
【问题讨论】:
-
forEach返回undefined,我认为Promise.all(...)不喜欢 -
Promise all 等待每个 Promise 流程成功解决。但是在 promise all 捕获第一个异常之后,catch where is attached。我认为您只想要已解决的值。但看起来你的一些承诺可能会被拒绝,不是吗?
-
您能否尝试将“拒绝”更改为“解决”,以便您可以按照预期进行操作。
标签: javascript node.js express promise