function getNode(subjectId, nodeId){
request(`http://hr.amiaodaifu.com:50000/1610/questions/${subjectId}/get-children/${nodeId}`, (err, res, body) => {
console.log(body)
if(err){
return console.log("err: ",err)
} else {
const content = JSON.parse(body);
console.log(content)
return content.length == 0 ? Promise.resolve(content) : Promise.resolve({
id: nodeId,
children: JSON.parse(body)
})
}
})
}
function getTree(subjectId, nodeId){
getNode(subjectId, nodeId)
.then(items => {
return Promise.map(items.children, item => (getTree(item)))
})
// .then((children) => ({
// id: nodeId,
// children
// }))
}
调用getTree的时候,提示的错误是:
Cannot read property 'then' of undefined
是我的思路有问题吗?
우선
request
의 반환값은Promise
이 아닙니다.두 번째로,
getNode
메소드는request
의 결과를 반환하지 않습니다.다음과 같이 변경할 수 있습니다.
으아악위층은 정답인데 약속이 엉뚱한 곳에서 쓰였습니다
Promise.map(items.children, item => (getTree(item))) 반환
Promise.map 기능이 있나요? 어떻게 사용하나요?