In this code,I am trying to view all the articles posted so far by the user .I added the columns dynamically whenever a new article is created by the user.So I created an array for storing only the articles which later on should be rendered on an ejs file.
app.get('/users/article/feed',checkAuthenticated,(request,response)=>{
let articleArray=[]
const sql="SELECT noOfArticles FROM Articles WHERE id=?"
db.query(sql,[request.user.id], async (err,result)=>{
if(err) throw err
let noOfArticles=result[0].noOfArticles
let promise=new Promise((resolve,reject)=>{
for(let i=1;i<=noOfArticles;i++){
const sql1="SELECT ?? FROM Articles WHERE id=?"
let index='article'+i
db.query(sql1,[index,request.user.id],(err,result)=>{
if(err) throw err
articleArray.push(result[0][index])
})
if(articleArray.length===noOfArticles){
resolve(articleArray);
}
} //end of for loop
})//end of promise function
let resultFinal=await promise;
console.log(resultFinal)
})
response.render('viewArticles',resultFinal)
})
I guess there is some mistake in the async await part. The resultFinal is not logging. How can I solve this