【问题标题】:node - TypeError: Cannot read property 'question' of undefined节点 - 类型错误:无法读取未定义的属性“问题”
【发布时间】:2021-07-19 03:57:20
【问题描述】:

我正在转换为客户端/服务器应用程序的 cli 应用程序有问题。

我编写的课程中有一个函数可以管理一些问题的随机抽取。有时我注意到该函数无法提取问题并出现此错误

/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)

这是导致问题的类的方法

    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 );
    }

问题是从在类的构造函数中加载的json 文件中加载的

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

有没有办法解决这个问题?

【问题讨论】:

    标签: javascript node.js arrays express random


    【解决方案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

    【讨论】:

      【解决方案2】:

      您的chance max 值错误,它应该比数组的length 小一。因为在10个元素的数组中,最大的索引是9。

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

      【讨论】:

      • 谢谢。我在想这个错误是由fs.readFileSync引起的!
      【解决方案3】:

      无法读取未定义的属性“问题”:发生此错误是因为您正在检查的问题属性可能不在构造函数中加载的问题(JSON)中。因此,请在分配并将其推送到 askQuestions 数组之前添加一个检查(如可选链接/if 条件检查/&&)

      【讨论】:

        猜你喜欢
        • 2017-11-01
        • 2022-11-21
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多