未处理的拒绝TypeError:无法读取属性' name'为null

时间:2017-03-01 09:20:20

标签: node.js

以下是我的代码:

router.post('/getTrainingProgramByTPType', function (req, res) {
    var query = { ... }; // simplified for clarity in stackoverflow

    models.TrainingProgram.findAll(query).then(function (trainingPrograms) {
        var resData = [];
        console.log(typeof req.body.userType, req.body.userType);

        trainingPrograms.forEach(trainingProgram => {
            if (trainingProgram.CourseType.name === req.body.userType || trainingProgram.CourseType.name === "EVERYONE" || (!req.body.isExperienced && trainingProgram.CourseType.name === "OPTIONAL")) {
                resData.push(trainingProgram);
            }
            else {
                var resDataCourse = [];
                var a;
                trainingProgram.Courses.forEach(course => {
                    course.Classes.forEach(classes => {
                        classes.ClassRecords.forEach(classRecord => {
                            if (classRecord.User.email === req.body.email) {
                                a = 1;
                                resDataCourse.push(course);
                            }
                        });
                    });
                });

                if (a === 1) {
                    resData.push({
                        id: trainingProgram.id,
                        name: trainingProgram.name,
                        description: trainingProgram.description,
                        imgLink: trainingProgram.imgLink,
                        courseTypeId: trainingProgram.courseTypeId,
                        CourseType: trainingProgram.CourseType,
                        Courses: resDataCourse
                    });
                }
            }
        });

        var datasend = {
            success: true,
            msg: 'send list success',
            trainingProgram: resData,
        };
        res.send(datasend);
    });
});

我收到了这个错误:

at Promise._settlePromiseFromHandler (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:522:17)
at Promise._settlePromise (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:567:18)
at Promise._settlePromise0 (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:612:10)
at Promise._settlePromises (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:691:18)
at Promise._fulfill (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:636:18)
at MappingPromiseArray.PromiseArray._resolve (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise_array.js:125:1 9)
at MappingPromiseArray._promiseFulfilled (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\map.js:101:18)
at Promise._settlePromise (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:572:26)
at Promise._settlePromise0 (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:612:10)
at Promise._settlePromises (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:691:18)
at Promise._fulfill (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:636:18)
at Promise._resolveCallback (C:\Users\huy.q.lam\Desktop\master\DEK-Talentnet\DCC\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:431:57)

可能导致TypeError的原因是什么?

1 个答案:

答案 0 :(得分:1)

我敢打赌trainingProgram.CourseType为空。在这种情况下,当您尝试访问trainingProgram.CourseType.name时,会收到Error

trainingPrograms.forEach(trainingProgram => {
    if (trainingProgram.CourseType.name === ...) { // <- ERROR HERE
        ....
    }
    ...
}

我不了解您的应用逻辑,但您可以尝试保护您的情况,例如:

if (trainingProgram.CourseType && trainingProgram.CourseType.name === ...) {