私はオブジェクトをループして、オブジェクトのキーに基づいて値を取得しようとしています。ループ/オブジェクトを処理する変数を使用しようとしています。私が値を前もって設定すると、データが得られます。私が "i"を通して反復によって定義された変数で値を設定しようとすると、定義されていないエラーが発生します。何かご意見は。破線はパグ用です。データはSails.jsクエリから取得されます。Pug.js Sails.js Javascript forループの問題
"0"を先に設定した場合。私はデータを取得します。
-var i=0;
-for (var key of Object.keys(thereviews.schoolreviews[0])) {
p= key + ": " + thereviews.schoolreviews[0].RID
-i++
-}
私は
-var i=0;
-for (var key of Object.keys(thereviews.schoolreviews[i])) {
p= key + ": " + thereviews.schoolreviews[i].RID
-i++
-}
にループを変更すると、私はこのエラーを取得する:
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
するとコンソールができます:それはあなたが試すことができない場合は
を.log 'Array.isArray(thereviews.schoolreviews)'あなたのコードはあまり意味がありません、あなたは何をしたいですか? – HMR