Search code examples
node.jsmongodbmongoosees6-promise

Why is promise.all returning an array within an array


I am coding on the backend (node & express) to make a request to the database (mongodb using mongoose)

When I run this code:

let RefundRequests = req.body.refundRequests.map(e => {return (

    Ticket.findById(e)

    )}  )

Promise.all(RefundRequests).then(data => {console.log('data', data) })

the console logs an array with an object:

    data [
     {
      //relevant data
     }
    ]

but when I change the promise to:

let RefundRequests = req.body.refundRequests.map(e => {return (

        Ticket.find({
                _id: e,
                refunded: false
            })
        )})

the same promise.all logs an array containing an array that contains an object:

data [
  [
    {
     //relevant data
    }
  ]
]

Why is there a difference? I would prefer if it returned one array with an object for ease of coding


Solution

  • find returns an array of documents, findById returns a single document. finding documents