문제

using bluebird q, I have this:

var myBill
db.getBillAsync().then(function (bill) {
myBill = bill
return users.find_user_by_idAsync(bill.user_id)
}).then(function (user) {
   myBill.user_name = user.name

console.log(myBill)

})

the purpose of the code above is to get the name of user and add it as to bill, this works well, now if I have a list of bills, how to get the names of all the bills and assign to myBills? using a loop? or bluebird has some other approaches?

var myBills
db.getBillsAsync().then(function (bills) {
  myBills = bills
  return users.find_user_by_idAsync(bill.user_id) ?
}).then(function (user) {
   ?
})
도움이 되었습니까?

해결책

It will look something like this:

db.getBillsAsync().map(function (bill) {
  return users.find_user_by_idAsync(bill.user_id)
    .then(function (user) {
      bill.user_name = user.name;
      return bill;
    });
}).then(function (bills) {
// you got your bills with user names
})
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top