0

So I am trying to experiment if how I will get all items in my 5 schema objects. Here how its look like.


const GetAllApple = (req,res) => {
    const list = []

    Ipad.find()
        .then(data => list.push(data))
        .catch(err => console.log(err))
    Mac.find()
        .then(data => list.push(data))
    Accessories.find()
        .then(data => list.push(data))
        .catch(err => console.log(err))
    HomeTools.find()
        .then(data => list.push(data))
    Phone.find()
        .then(data => list.push(data))
        .catch(err => console.log(err))

    console.log(list)
    res.send(list)
    
}

But it doesn't work, It is giving me an empty array and also a empty output in my postman but it doesn't give any error.

But when I try to do this thing I comment out the Ipad, Mac, HomeTools and Phone My Accessories should something like this and the console.log(list)

    Accessories.find()
        .then(data => 
            {
            list.push(data);
            res.send(data)
        })
        .catch(err => console.log(err))

    console.log(list)

The output in terminal is still empty list [] but in my postman

[
    {
        "_id": "624da84b97cdc2714b8fd5f5",
        "itemname": "ACBASDASD",
        "itemno": 122,
        "image": "123123.jpg",
        "createdAt": "2022-04-06T14:48:43.515Z",
        "updatedAt": "2022-04-06T14:48:43.515Z",
        "__v": 0
    },
    {
        "_id": "624dac788a77d5ea59650f1e",
        "itemname": "ACBASDASD",
        "itemno": 122,
        "image": "gray.jpg",
        "createdAt": "2022-04-06T15:06:32.173Z",
        "updatedAt": "2022-04-06T15:06:32.173Z",
        "__v": 0
    }
]

It is something like this..Anyone have an idea? I wanted to try this maybe there is another or whatsoever you may think about.. I know this kinda feel odd as for me too but I wanna try this new different ideas lol but it doesn't work very well.

EDITED I also tried something like this

    Ipad.find()
        .then(data => res.send(data))
        .catch(err => console.log(err))

    Mac.find()
        .then(data => res.send(data))
        .catch(err => console.log(err))

    Accessories.find()
        .then(data => res.send(data))
        .catch(err => console.log(err))
        
    HomeTools.find()
        .then(data => res.send(data))
        .catch(err => console.log(err))

    Phone.find()
        .then(data => res.send(data))
        .catch(err => console.log(err))

but that feels illegal since I am calling the res callback multiple times.

MYTH
  • 125
  • 9

2 Answers2

1

You can use promise chain, Promise.all syntax, or async/await syntax:

Promise chain:

const GetAllApple = (req, res) => {
  const list = []

  Ipad.find()
    .then(data => {
      list.push(data)
      return Mac.find()
    })
    .then(data => {
      list.push(data)
      return Accessories.find()
    })
    // ...
    .then(data => {
      list.push(data)
      console.log(list)
      res.send(list)
    })
    .catch(err => {
      console.log(err)
      res.status(500).send(err)
    })
}

Promise.all

const GetAllApple = (req, res) => {
  Promise.all([
    Ipad.find(),
    Mac.find(),
    // ...
  ])
    .then(list => {
      console.log(list)
      res.send(list)
    })
    .catch(err => {
      console.log(err)
      res.status(500).send(err)
    })
}

async/await

const GetAllApple = async (req, res) => { // async function
  try {
    const ipads = await Ipad.find() // await
    const macs = await Mac.find()
    // ...
    res.send([ipads, macs]) // or res.send([...ipads, ...macs])
  } catch(err) {
    console.log(err)
    res.status(500).send(err)
  }
}
hoangdv
  • 15,138
  • 4
  • 27
  • 48
0

You might need to deserialize your response object

Ipad.find()
     .then(res => {
          const data = res.json();
          list.push(data);
     })
     .catch(err => console.log(err))
  • This doesn't work. It gives me `res is not a function` but I did tried your answer something lkes this ``` Accessories.find() .then(data => { const item = res.json(data) list.push(item) }) .catch(err => console.log(err)) ``` but still give me the same output as I mention in my first code snippet. – MYTH Apr 07 '22 at 02:42
  • Just think about it pushing the objects in the array. I am thinking about parsing it but I don't know how to do it lol. – MYTH Apr 07 '22 at 02:50
  • Nevermind I already find a way with this link hahah https://stackoverflow.com/questions/56667583/coremongoosearray-to-normal-array – MYTH Apr 07 '22 at 03:13