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

问题描述 投票:0回答:1

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

  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中的值?

node.js mongodb promise mongodb-query
1个回答
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

推荐问答