내가 사용하는 기술은 Node.js, express 및 MongoDB입니다.
모든 레시피에 대해 데이터베이스를 쿼리하는 'getAllRecipes'라는 메서드가 있습니다. 이것은 잘 작동합니다.
'getAllRecipes'콜백 메서드에서 각 항목 / 레시피를 반복하여 메모와 주석을 얻습니다. 루프 내부의 작업은 'let'을 모방하기 위해 클로저 내부에 배치됩니다. 그렇지 않으면 모든 recipeID가 동일합니다.
이제 'async.series'와 함께 사용할 배열에 대한 추가 호출을 추가합니다. 우선 'getRecipeNotes'를 호출하고 이것이 반환되면 'getRecipeNotes'콜백 메서드에서 'getRecipeComments'를 호출합니다.
최종 출력은 다음과 같은 내용을 보여줍니다.
[
{
"_id": "56d070ac7c3965e85d3ebd38",
"userID": "56cf383a1f8303082484f35f",
"name": "Chocolate cake",
"instructions": "Add mixture to bowl, and then mix",
"dateTimeSubmitted": "2016-02-26T15:35:08.868Z",
"tags": "cooking, cake, chocolate",
"active": 1,
"notes": [],
"comments": []
},
{
"_id": "56d070ac7c3965e85d3ebd3a",
"name": "num1",
"quantity": "5",
"notes": [],
"comments": []
}, ...]
모든 '노트'와 모든 '코멘트'는 빈 배열입니다.
댓글 모음을 확인한 결과 recipeID : "56d070ac7c3965e85d3ebd38"에 대한 항목이 있습니다.
각 레시피의 모든 주석을 반환하도록 'getRecipeComments'메서드를 수정하면 모두 반환됩니다.
또한 recipeID를 사용하여 쿼리를 유지하고 recipeID : "56d070ac7c3965e85d3ebd38"을 사용하여 getAllRecipes 메소드 외부에서 호출하면 데이터도 잘 반환됩니다. getAllRecipes 메소드를 사용하지 않습니다.
getAllRecipes :
function getAllRecipes( db, callback )
{
//query( db, {}, 'recipes', callback );
query( db, {}, 'recipes', function( err, recipeData )
{
tempArray = recipeData.slice();
if( err || !tempArray.length )
callback( err );
else
{
var taskArr = [];
for( var i=0; i < tempArray.length; i++ )
{
(function()
{
var tID = tempArray[ i ]._id;
taskArr.push( function( iCallback )
{
getRecipeNotes( db, tID, function( err2, noteData )
{
getRecipeComments( db, tID, function( err3, commentData )
{
if( err2 )
{
iCallback( err2 );
return;
}
if( err3 )
{
iCallback( err3 );
return;
}
for( var j=0; j < tempArray.length; j++ )
{
if( tempArray[ j ]._id === tID )
{
tempArray[ j ].notes = noteData;
tempArray[ j ].comments = commentData;
}
}
iCallback();
});
});
});
})();
}
async.series( taskArr, function( err )
{
callback( err, tempArray );
} );
}
});
}
getRecipeComments :
function getRecipeComments( db, recipeID, callback )
{
query( db, { recipeID : recipeID }, 'comments', callback );
}
질문:
function query( db, query, strColl, callback )
{
var coll = db.get( strColl );
coll.find( query, function( err, doc )
{
if( err )
console.log( err );
callback( err, doc );
});
}
또한 'getRecipeNotes'및 'getRecipeComments'에 대한 호출을 두 개의 개별 작업으로 분리했지만 결과는 동일합니다.
메모 나 댓글이 반환을 거부하는 이유를 알 수 있습니까?
문제가 해결되었습니다. 데이터베이스 쿼리 호출에 대한 유형 오류 인 것 같습니다.
나는 단지 간단한 일이 필요했습니다.
recipeID = recipeID.toString();
쿼리 전
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다