0

I am trying to save in a variable the data received from a request to a mongoDb database with this code:

var riskRatingTable = [];
RiskRating.find((err, tableData) => {
    if (err)
        console.log(err);
    else
    riskRatingTable = tableData;
});
//This returns undefined
console.log('riskRatingTable: ', riskRatingTable);

After having made some research, I have found that this solvable by using callback or promises: Solving js asynchronous functions problems So I tried this:

 var riskRatingTable = [];
    RiskRating.find((err, tableData) => {
        if (err)
            console.log(err);
        else
        riskRatingTable = tableData;
    }).then((tableData)=>{
        riskRatingTable = tableData;
s    });
    console.log('riskRatingTable: ', riskRatingTable);

But, I get this error:

(node:2916) UnhandledPromiseRejectionWarning: ReferenceError: s is not defined

Any idea how I can fix this?
Thank you !!

Ahmed Ghrib
  • 695
  • 4
  • 13
  • 29

0 Answers0