質問

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