1

Have a look on my controller

exports.userList = async (req, res) => {
   let result = await Methods.getAllData(Campaign)
   await console.log(result, 'tr')
}

Here Methods.getAllData is a function to get all user data and the parameter Campaign is my Model name.

here the function..

methods.getAllData = (modelName)=>{
    modelName.findAll({
    }).then(value=>{
        console.log(value, 'rt')
        return value
    }).catch(err=>{
        return err
    })
}
  1. My console.log statement is running first. I have tried to solve using aync/await but not working. I need to get the result and then I need to console that.

  2. Here I have returned the data, how can I use callback as replacement of return?

Rabbani_
  • 450
  • 1
  • 10
  • 30

3 Answers3

1

You just need to return promise from getAllData

const getAllData = (modelName)=>{
  return  modelName.findAll();
}

exports.userList = (req, res) => {
   const newPromise = getAllData(Campaign);

   newPromise
     .then( result => console.log('result',result))
     .catch( err => console.log('error', err))

}
Prakash Karena
  • 2,525
  • 1
  • 7
  • 16
1

All you need to do is to return the promise generated by findAll(). You do not need to change anything else in your code:

methods.getAllData = (modelName)=>{
    return modelName.findAll({ // ---------------- this is the  bug fix
    }).then(value=>{
        console.log(value, 'rt')
        return value
    }).catch(err=>{
        return err
    })
}

However, returning from a catch like this is bad practice because it converts the error into a regular return value. Either rethrow the error:

    }).catch(err=>{
        throw err
    }) 

or don't catch errors here:

methods.getAllData = (modelName)=>{
    return modelName.findAll({ // ---------------- this is the  bug fix
    }).then(value=>{
        console.log(value, 'rt')
        return value
    }) // ---------------------------------------- remove catch
}
slebetman
  • 109,858
  • 19
  • 140
  • 171
0

You can change your getAllData method with this code below:

Updated:

methods.getAllData = (modelName)=>{
  return modelName.findAll();
}

And in your userList, you can make it like this code below:

exports.userList = async (req, res) => {
  try {
    let result = await Methods.getAllData(Campaign);
    console.log(result);
  } catch(ex) {
    console.log(ex.message);
  }
}

I hope it can help you.

Titus Sutio Fanpula
  • 3,467
  • 4
  • 14
  • 33
  • The function `findAll` already returns a promise. Don't wrap it in another promise - this is an anti-pattern – slebetman Jan 30 '20 at 07:17
  • The function `findAll` already returns a promise. You do not need to await it. Just return it – slebetman Jan 30 '20 at 07:18
  • @Rabbani_ Your bug is you forgot to return the promise. You called a function that returns a promise then you don't return anything at all – slebetman Jan 30 '20 at 07:27
  • 1
    There could be other reasons, for example the OP appears to always run a `then()` on `findAll()` no matter who calls `getAllData()`. Or it could just be adhering to specific design patterns such as the repository pattern which disallows you calling database functions directly etc. In any case, you are also "creating another function to put a model in" - your argument goes against your own answer. Wrapping a promise in a promise constructor is a well-known anti-pattern and bad coding practice: https://stackoverflow.com/questions/53910226/understanding-explicit-promise-construction-anti-pattern – slebetman Jan 30 '20 at 07:48
  • @slebetman: Thank you for your advice. I really appreciate it. – Titus Sutio Fanpula Jan 30 '20 at 07:53