gpt4 book ai didi

node.js - 未处理的拒绝 TypeError : Cannot read property 'name' of null

转载 作者:行者123 更新时间:2023-12-02 01:16:07 25 4
gpt4 key购买 nike

以下是我的代码:

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?

最佳答案

我打赌 trainingProgram.CourseType 为空。在这种情况下,当您尝试访问 trainingProgram.CourseType.name 时,您会收到一个错误:

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

我不明白你的应用程序逻辑,但你可以尝试保护你的条件不受这种情况的影响,例如:

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

关于node.js - 未处理的拒绝 TypeError : Cannot read property 'name' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42527772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com