Pug.js Sails.js Javascript for loop issue

99 Views Asked by At

I am trying to loop through an object to get a value based on the key in the object. I am trying to use a variable to work through the loop/object. If I set the value ahead of time I get the data. If I try to set the value with a variable defined by an iteration through "i" I get an undefined error. Any thoughts. The dashed are for pug. The data is coming from a Sails.js query.

If I set the "0" ahead of time. I get data.

-var i=0;
  -for (var key of Object.keys(thereviews.schoolreviews[0])) {
    p= key + ": " + thereviews.schoolreviews[0].RID
  -i++
  -}

If I change the loop to

-var i=0;
  -for (var key of Object.keys(thereviews.schoolreviews[i])) {
    p= key + ": " + thereviews.schoolreviews[i].RID
  -i++
  -}

I get this error:

TypeError: /Users/mnelson/Documents/Personal/My Design Musings/sails/MIE Sails/views/reviews/reviews.pug:8
    6|       -var i=0;
    7|       -for (var key of Object.keys(thereviews.schoolreviews[i])) {
  > 8|         p= key + ": " + thereviews.schoolreviews[i].RID
    9|       -i++
    10|       -}
    11|       table.table

Cannot read property 'RID' of undefined
at eval (eval at wrap (/Users/mnelson/Documents/Personal/My Design Musings/sails/MIE Sails/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:145:98)
    at template (eval at wrap (/Users/mnelson/Documents/Personal/My Design Musings/sails/MIE Sails/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:342:101)
    at /usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:808:16
    at /usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:143:5
    at Promise._execute (/usr/local/lib/node_modules/sails/node_modules/bluebird/js/release/debuggability.js:303:9)
    at Promise._resolveFromExecutor (/usr/local/lib/node_modules/sails/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/usr/local/lib/node_modules/sails/node_modules/bluebird/js/release/promise.js:79:10)
    at promisify (/usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:136:10)
    at exports.pug (/usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:792:10)
    at SailsView.sails.config.views.engine.fn [as engine] (/usr/local/lib/node_modules/sails/lib/hooks/views/configure.js:83:7)
    at SailsView.View.render (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/view.js:76:8)
    at Function.app.render (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/application.js:561:10)
    at ServerResponse.res.render (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/response.js:845:7)
    at ServerResponse.res.view (/usr/local/lib/node_modules/sails/lib/hooks/views/res.view.js:284:16)
    at /Users/mnelson/Documents/Personal/My Design Musings/sails/MIE Sails/api/controllers/ReviewsController.js:14:20
    at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:58:28)
    at returnResults (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:179:9)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:91:16
2

There are 2 best solutions below

0
On

You need an outer loop because the for() is just iterating over the object's keys and having the i++ inside of the for() could cause an index out of range exception. Also, I removed the hardcoded RID and am using the [key] accessor in its place which is safer, incase the data is malformed.

- var i=0;
- while(i < thereviews.schoolreviews.length)
  - for (var key of Object.keys(thereviews.schoolreviews[i])) {
    p= key + ": " + thereviews.schoolreviews[i][key]
  - }
  - i++
0
On

I'm not sure if shoolreviews is an array but if it is you can do the following:

console.log(
  thereviews.schoolreviews
  .map(
    (item,index) => `${index}: ${item.RID}`
  )
);

If it's not you can try:

console.log(
  Object.keys(thereviews.schoolreviews)
  .map(
    (key) => `${key}: ${thereviews.schoolreviews[key].RID}`
  )
);