함수에서 문서의 데이터에 액세스하고 싶습니다. 항목을 처리했는데 어떻게 itemstring에 변수 document.date를 넣을 수 있습니까?부모 함수에서 변수에 액세스하려면 어떻게해야합니까?
function pagelist(items, res) {
var db = db_login;
result = "<html><body><ul>";
items.forEach(function(item) {
console.log(item)
db.collection('insights').findById(item._id , function(error, document) {
console.log(document)
if (error || !document) {
res.render('error', {});
} else {
**console.log(document.date) //this value is displayed**
}
})
**console.log(document.date)//this value is undefind**
itemstring = "<li>" + item._id + "<ul><li>" + item.textScore +
"</li><li>" + item.created + "</li><li>" + item.document +
"</li></ul></li>";
result = result + itemstring;
});
result = result + "</ul></body></html>";
return result;
}
'findById'이 비동기이기 때문에,이 관련 질문을 참조 : HTTP : // 유래 .com/questions/23667086/왜 - 내 - 변수 - 변경되지 않은 - 이후 - 수정 - 그것 - 기능 - 비동기 - 내부 – JohnnyHK