Promise.all不适用于两个mongodb查询

时间:2019-12-03 00:50:27

标签: node.js mongodb promise mongodb-query

我正在尝试将后端的三个数据合并到一个对象数组中。我试图弄清楚是否有足够的门票来进行销售。这三个数据是:

  1. 来自前端的请求,用于为活动购买一定数量的某种类型的门票。这是一个对象数组。

  2. 该活动已售出的该类型门票的总数。

  3. 可从我在MongoDB中创建的事件文档中出售的那种类型的门票数量。

这正在使用以下代码工作。但是,它是同步的,我想异步进行以加快站点速度。

let numTicketsSought = req.body.numTicketsSought.map(e => { return(

    Ticket.find({
        userEvent: req.body.userEvent,
        ticketTypeID: e.ticketTypeID
    }).lean().countDocuments()
    .then(number => {
        e.ticketsSold = number
        return e
    }))

}) 

Promise.all(numTicketsSought).then(numTicketsSoughtdata => {
    Event.findById(req.body.userEvent)
        .lean()
      .then(eventdata => {
            numTicketsSoughtdata.map(e => {return (
                eventdata.tickets.forEach(f => {
                    if (e.ticketTypeID === f.ticketTypeID){
                        e.numberOfTickets = f.numberOfTickets
                        e.ticketsAvailable = e.numberOfTickets - e.ticketsSold
                        return e
                    }
                })
            )})

})

但是,当我尝试异步运行Ticket.find和Event.findbyID(使用Promise.all)时,代码将无法运行。这是代码:

    let numTicketsSought = req.body.numTicketsSought.map(e => { return(

    Ticket.find({
        userEvent: req.body.userEvent,
        ticketTypeID: e.ticketTypeID
    }).lean().countDocuments()
    .then(number => {
        e.ticketsSold = number
        return e
    }))

})

let eventInfo = Event.findById(req.body.userEvent).select('tickets.ticketTypeID tickets.numberOfTickets')
    .then().catch(err => console.log(err))

    Promise.all([numTicketsSought, eventInfo]).then(data => {
let numTicketsSought = data[0]
let eventInfo = data[1]
console.log('numTicketsSought', numTicketsSought)
console.log('eventInfo', eventInfo)
numTicketsSought.map(e => {return (

                eventInfo.tickets.forEach(f => {
                    console.log('TRIGGERED')
                    console.log('e.ticketTypeID ', e.ticketTypeID )
                    console.log('f.ticketTypeID ', f.ticketTypeID )
                    console.log('----------------')
                    if (e.ticketTypeID === f.ticketTypeID){
                        console.log('MATCHED')
                        e.numberOfTickets = f.numberOfTickets
                        e.ticketsAvailable = e.numberOfTickets - e.ticketsSold
                        return e
                    }
                })
            )})

})

这是终端显示的内容:

numTicketsSought [ Promise {
    { ticketType: 'Early Bird',
      ticketTypeID: 1,
      numTicketsSought: 1,
      ticketsSold: 100 } },
  Promise {
    { ticketType: 'Early Bird VIP',
      ticketTypeID: 2,
      numTicketsSought: 2,
      ticketsSold: 15 } } ]
eventInfo { _id: 5de147e1ed01a505f1ee0011,
  tickets:
   [ { ticketTypeID: 1, numberOfTickets: 5 },
     { ticketTypeID: 2, numberOfTickets: 5 },
     { ticketTypeID: 3, numberOfTickets: 10 },
     { ticketTypeID: 4, numberOfTickets: 4 } ] }
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  1
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  2
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  3
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  4
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  1
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  2
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  3
----------------
TRIGGERED
e.ticketTypeID  undefined
f.ticketTypeID  4
----------------

我在做什么错,这阻止了Promise.all使用第一个Promise中的值?

1 个答案:

答案 0 :(得分:0)

我认为这是因为您有Promises的数组而不是单个Promise

let numTicketsSought = req.body.numTicketsSought.map(e => { return(

    Ticket.find({
        userEvent: req.body.userEvent,
        ticketTypeID: e.ticketTypeID
    }).lean().countDocuments()
    .then(number => {
        e.ticketsSold = number
        return e
    }))

}) 

这里的问题是Array.prototype.map将返回一个数组。

Promise.All([]),如果您正在做Promise.all([[],]),则需要解开承诺并将其全部连接起来

numTicketsSought.push(eventInfo);
Promise.all(numTicketsSought) // <-- rename numTicketsSought to something better like promisesToComplete