簡體   English   中英

節點 - 類型錯誤:無法讀取未定義的屬性“問題”

[英]node - TypeError: Cannot read property 'question' of undefined

我正在轉換為客戶端/服務器應用程序的 cli 應用程序有問題。

我在 class 中有一個 function 我寫的它將管理一些問題的隨機提取。 有時我注意到 function 無法提取問題,我收到此錯誤

/Users/dev/Desktop/demo/demo-gui.js:77
                question: this.questions[n].question,
                                            ^

TypeError: Cannot read property 'question' of undefined
    at DemoGUI.extractQuestions (/Users/dev/Desktop/demo/demo-gui.js:77:45)
    at /Users/dev/Desktop/demo/demo-gui.js:51:18
    at Layer.handle [as handle_request] (/Users/dev/Desktop/demo/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dev/Desktop/demo/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/dev/Desktop/demo/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/dev/Desktop/demo/node_modules/express/lib/router/layer.js:95:5)
    at /Users/dev/Desktop/demo/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/dev/Desktop/demo/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/dev/Desktop/demo/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/Users/dev/Desktop/demo/node_modules/body-parser/lib/types/urlencoded.js:91:7)

這是導致問題的 class 的方法

    async extractQuestions(){
        this.askQuestions = [];
        this.questionsAnswers = [];


        for(let i = 0; i < 10; i++){
            let n = chance.integer({min: 0, max: this.questions.length});

            this.askQuestions.push({
                questionIndex: n,
                question: this.questions[n].question,
                choices: this.questions[n].possibleAnswers
            });

            this.questionsAnswers.push({
                index: n,
                correctAnswer: this.questions[n].correctAnswer 
            });
        }
        //this.maxScore = Math.floor( 5 * 10 );
    }

問題是從 class 的構造函數中加載的json文件加載的

    constructor(questionsFile, uiResources){
        this.questionsData = fs.readFileSync(questionsFile, {encoding: 'utf-8'});
        this.questions = JSON.parse(this.questionsData);
        this.uiResources = uiResources;
        this.app = express();
    }

有沒有辦法解決這個問題?

你的chance max是錯誤的,它應該比數組的length小一。 因為在 10 個元素的數組中,最大的索引是 9。

let n = chance.integer({min: 0, max: this.questions.length - 1});

也許optional chaining和驗證您的數據可以為您節省:

async extractQuestions(){
// Early return if your data is not an Array or has lenght under 1
      if(Array.isArray(this.questions) && this.questions.length < 1) return;

        this.askQuestions = [];
        this.questionsAnswers = [];


        for(let i = 0; i < 10; i++){
            let n = chance.integer({min: 0, max: this.questions.length - 1});

            this.askQuestions.push({
                questionIndex: n,
                question: this.questions[n]?.question,
                choices: this.questions[n]?.possibleAnswers
            });

            this.questionsAnswers.push({
                index: n,
                correctAnswer: this.questions[n]?.correctAnswer 
            });
        }
        //this.maxScore = Math.floor( 5 * 10 );
    }

參考: https://javascript.info/optional-chaining

無法讀取未定義的屬性“問題”:發生此錯誤是因為您正在檢查的問題屬性可能不存在於構造函數中加載的問題(JSON)中。 因此,請在分配並將其推送到 askQuestions 數組之前添加一個檢查(如可選鏈接/if 條件檢查/&&)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM