2016-11-17 1 views
0

콜백 외부에서 variable inventory에 액세스하려면 어떻게해야합니까? 나는 그것에 접근하여 그것을 돌려주고 싶다. 당신은 loadInventory 콜백을 수행해야nodejs 콜백 메소드 내 변수에 액세스

loadInventory = function() { 

var inventory = []; 

offers.loadMyInventory({ 
    appId: 730, 
    contextId: 2, 
    tradableOnly: true 

}, function (err, items) { 
    items.forEach(function (item) { 
     inventory.push({ 
      asset_id: item.id, 
      market_name: item.market_name 
     }); 
    }); 
    //Not accessible here 
}); 
}; 
+0

// 여기에 액세스 할 수 위의 콜백 내에서 – Joe

+0

당신이 작업을 공유 할 수 있습니다하지 코드 그래서 내가 할 수있는 그것을 실행? –

답변

0

, 그것을 호출간에 그 통과 할 것 :

loadInventory = function (callback) { 

var inventory = []; 

offers.loadMyInventory({ 
    appId: 730, 
    contextId: 2, 
    tradableOnly: true 

}, function (err, items) { 
    items.forEach(function (item) { 
     inventory.push({ 
      asset_id: item.id, 
      market_name: item.market_name 
     }); 
    }); 
    callback(inventory); 
}); 
}; 

loadInventory(function(inventory) { 
    console.log(inventory); 
}); 
+0

해결했습니다. 감사합니다. – T3rraform

+0

대답으로 표시 할 수 있습니까? – Joe