skip to Main Content

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.

2

Answers


  1. You might need to deserialize your response object

    Ipad.find()
         .then(res => {
              const data = res.json();
              list.push(data);
         })
         .catch(err => console.log(err))
    
    Login or Signup to reply.
  2. 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)
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search