【问题标题】:how to get users names using bluebird?如何使用蓝鸟获取用户名?
【发布时间】:2014-05-12 01:10:51
【问题描述】:

使用 bluebird q,我有这个:

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)

})

上面代码的目的是获取用户名并将其添加到账单中,这很好用,现在如果我有一个账单列表,如何获取所有账单的名称并分配给 myBills?使用循环?还是蓝鸟有其他方法?

var myBills
db.getBillsAsync().then(function (bills) {
  myBills = bills
  return users.find_user_by_idAsync(bill.user_id) ?
}).then(function (user) {
   ?
})

【问题讨论】:

    标签: node.js promise q bluebird


    【解决方案1】:

    它看起来像这样:

    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
    })
    

    【讨论】:

    • 谢谢,但 bill.user_name = user.name 似乎不是指地图中的“账单”?
    • 为什么?范围内没有其他bill
    猜你喜欢
    • 2018-10-23
    • 2015-09-06
    • 2015-07-01
    • 2015-04-13
    • 1970-01-01
    • 2019-09-29
    • 2017-06-18
    • 2015-07-18
    • 2016-07-14
    相关资源
    最近更新 更多