0
개체 목록을 검색하여 내 모바일 앱으로 다시 보내려고합니다. 난 그냥 후 forEach 루프가 끝난 후 실제로 그들을 보내는 데 약간의 어려움이 있습니다.Node.JS가 루프에서 API 호출 후 응답을 보냅니다.
배열에 해당 변수 "data"를 추가하고 루프 외부로 보냈지 만 배열이 비어 있습니다. 분명히 데이터가 검색되지만 배열에 제 시간에 푸시되지 않습니다.
res.send()를 호출하기 전에 루프를 끝내려면 어떻게해야합니까? 가능한 한 간단하게 코드를 줄였습니다.
var stripe = require("stripe")("stripe_key");
exports.fetchTransactions = function(req, res) {
var account = req.body.account;
stripe.transfers.list({ destination: account}, function(err, transactions) {
transactions.data.forEach(function(item) {
stripe.transfers.retrieve(item.id, function(err, transfer) {
if (err) {
console.log(err);
};
var data = {
amount: item.amount,
created: item.created
};
// Can't call res.send(data) inside the loop because res.send() can only be called once.
// But if I call res.send(array) outside of the loop, the array is still empty
});
});
});
};
정말 고마워요. – Lucas