0

My project code has been working perfectly fine 5 months ago. Now I am not getting why handlebars is not showing data in any list. I have checked backend, where I am getting array of categories and i can console.log elements of objects inside the array. Here is my backend code:

// shows category list
exports.categoryList = async( req, res )=> {
  var category = await Cat.find()
  var count = 1
  category.map( doc=> doc.count = count++  )
  console.log(category)
  console.log(category[0].name)
  res.render('parents/categoryList', { category })
}

Output of these console.log:

[ { subCategories: [],
    brands: [],
    enabled: false,
    discount: null,
    _id: 5e6f2a069894412b749e2fca,
    name: 'abcd',
    created: 2020-03-16T07:25:58.335Z,
    __v: 0 } ]
abcd

In handlebar I have tried printing category variable which I have passed from backend:

<p>{{category}}</p>

{{#each category}}

count: {{this.count}} <br>
id: {{this._id}}<br>

{{/each}}

Front-end output:

{ subCategories: [], brands: [], enabled: false, discount: null, _id: 5e6f2a069894412b749e2fca, name: 'abcd', created: 2020-03-16T07:25:58.335Z, __v: 0 }
count: 1
id: 

As you can see the count variable what I have set in the backend code is not inside the category object but its printing while iterating through the category array. Could anyone tell me what's went wrong?

Fathma Siddique
  • 266
  • 3
  • 15

1 Answers1

0

.map will return array. If you save that array in some variable I believe it will work.

const updArr = category.map( doc=> {
  const newDoc = {}
  newDoc = doc;
  newDoc.count = count++
  return newDoc;
})
console.log(updArr) // check output here
res.render('parents/categoryList', { updArr })

On fronted try to console this object. I think this object is the response of your api that you are getting it from backend. And this.count is the count of your records.

Thanks

Qubaish Bhatti
  • 700
  • 5
  • 22
  • Not working. The updArr is showing [1,2,3,4,5] in console. But i had a query result in category, which is being overridden. – Fathma Siddique Mar 18 '20 at 06:46
  • now I am getting query result objects with count...which is ok. But in the handlebars only count is showing but other object elements like name, _id are not showing. my problem is, in the given code, back and front both were working perfectly. i had only fixed some vulnerabilities of that project. which might causing the problem. – Fathma Siddique Mar 18 '20 at 07:07
  • just try console (this) on frontend and let me know what it is printing. – Qubaish Bhatti Mar 18 '20 at 07:23
  • As I understand your code. You are getting object on frontend not the array. Try {{category._id}} – Qubaish Bhatti Mar 18 '20 at 07:33
  • {{category._id}} with ur code shows nothing. with my code shows nothing. (i have more than one object in the array) – Fathma Siddique Mar 18 '20 at 07:52
  • Check this: https://stackoverflow.com/questions/22696886/how-to-iterate-over-array-of-objects-in-handlebars – Qubaish Bhatti Mar 18 '20 at 08:11