-1

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

loksan
  • 157
  • 3
  • 17
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – CertainPerformance May 27 '20 at 13:39
  • You're checking `articleArray.length` synchronously, before any of the requests go out – CertainPerformance May 27 '20 at 13:40
  • How do I solve it then?? – loksan May 27 '20 at 13:42
  • By checking the length *after* the requests come back. You should also handle the errors properly - currently, if there's an error, the Promise will never resolve – CertainPerformance May 27 '20 at 13:43
  • Can you post the correct answer ? I am a newbie to js .Am not able to get the output after adding try catch statements ? – loksan May 27 '20 at 14:27

1 Answers1

0

from my point of view you are using callback inside callbacks and this is hard to understand, I will post a refactor of your code maybe can help you to figure out what is happing

app.get('/users/article/feed', checkAuthenticated, async (request, response) => {
  let articleArray = []

  const sql1 = 'SELECT noOfArticles FROM Articles WHERE id=?'
  const result1 = await db.query(sql1, [request.user.id])

  let noOfArticles = result1[0].noOfArticles

  for (let i = 1; i <= noOfArticles; i++) {
    const sql2 = 'SELECT ?? FROM Articles WHERE id=?'
    let index = 'article' + i
    const result2 = await db.query(sql1, [index, request.user.id])

    articleArray.push(result2[0][index])
  } //end of for loop


  let resultFinal = articleArray
  console.log(resultFinal)

  response.render('viewArticles', resultFinal)
})