0

I have a sails js app, The following codes works fine:

list: async(req, res) => {
    Data.find({}).exec((err, data)=>{
      if(err){
        res.send(500, {message: 'db error'});
      }
      res.status(200).json({
        message: 'Data List',
        data: data
      });
    });

Outputs all the data of the collection correctly. While the below code removes all the data from the mongo db collection and then shows a empty array:

list: async(req, res) => {
    const data = await Data.find({});
    if(!data){
      res.send(500, {message: 'db error'});
    }
    res.status(200).json({
      message: 'Data List',
      data: data
    });
  }

I do not understand why so, I am more comfortable with async await also it makes code look cleaner hence I wanted to use the below method. Please help how can I make the below code snippet to work just like the above one.

Vinita
  • 1,834
  • 2
  • 8
  • 20

1 Answers1

1

It worked fine, when used

const data = await Data.find();

instead of

const data = await Data.find({});
Vinita
  • 1,834
  • 2
  • 8
  • 20