我正在使用 TypeScript 在前端 CRM 系统中执行一些基本计算。
我遇到的问题是“TypeScript-类型‘null’不能用作索引类型。”虽然在上面的 IF 语句中检查分数选项是否为空。
请查看我的代码,我尝试过使用/不使用变量进行实验。错误出现在 A/B/C 部分 Score = ScoreMap[ScoreOptionA];百分比也一样。
export async function CalculateAverageScore() {
console.log(`CalculateAverage JS Triggered`);
try {
console.log(`Calculate Average Score JS try`);
const ScoreOptionA: Xrm.Attributes.OptionSetAttribute = FORM_CONTEXT.getAttribute("dp_testweightedaverage");
const PercentageOptionA: Xrm.Attributes.OptionSetAttribute = FORM_CONTEXT.getAttribute("dp_percentagevalue");
const ScoreOptionB: Xrm.Attributes.OptionSetAttribute = FORM_CONTEXT.getAttribute("dp_average2");
const PercentageOptionB: Xrm.Attributes.OptionSetAttribute = FORM_CONTEXT.getAttribute("dp_percentagevalue2");
const ScoreOptionC: Xrm.Attributes.OptionSetAttribute = FORM_CONTEXT.getAttribute("dp_pickascorefirmwide");
const PercentageOptionC: Xrm.Attributes.OptionSetAttribute = FORM_CONTEXT.getAttribute("dp_percentagevaluefirmwide");
// Define Scoring & Percentages
const scoreMap: {
[Score: number]: number
} = {
778180000: 1,
778180001: 2,
778180002: 3,
778180003: 4,
778180004: 5
};
const percentageMap: {
[Percentage: number]: number
} = {
778180000: 0.10,
778180001: 0.20,
778180002: 0.30,
778180003: 0.40,
778180004: 0.50
};
FORM_CONTEXT.getAttribute<Xrm.Attributes.OptionSetAttribute>('preferredcontactmethodcode')?.getValue();
var SectionAScore, SectionAPercentage, SectionBScore, SectionBPercentage, SectionCScore, SectionCPercentage;
// Section A Score & Percentages
if (ScoreOptionA != null && PercentageOptionA != null) {
ScoreOptionA.getValue();
PercentageOptionA.getValue();
SectionAScore = scoreMap[ScoreOptionA];
SectionAPercentage = scoreMap[ScoreOptionB];
}
//Section B Set Score & Percentages
if (ScoreOptionB != null && PercentageOptionB != null) {
var ScoreValueB = ScoreOptionB.getValue();
var valuePercentageB = PercentageOptionB.getValue();
SectionBScore = scoreMap[ScoreValueB];
SectionBPercentage = percentageMap[valuePercentageB];
}
// Section C Score & Percentages
if (ScoreOptionC != null && PercentageOptionC != null) {
var ScoreValueC = ScoreOptionC.getValue();
var valuePercentageC = PercentageOptionC.getValue();
SectionCScore = scoreMap[ScoreValueC];
SectionCPercentage = percentageMap[valuePercentageC];
}
// Calculations
var Section1Score = (SectionAScore * SectionAPercentage);
var Section2Score = (SectionBScore * SectionBPercentage);
var Section3Score = (SectionCScore * SectionCPercentage);
var averagescore = (Section1Score + Section2Score + Section3Score);
// Set Average Score Value
FORM_CONTEXT.getAttribute("dp_decimal").setValue(averagescore);
}
catch (error) {
console.log(`exampleFunction error`, error);
HELPERS.displayErrorMessage(error);
}
finally {
console.log(`exampleFunction finally`);
}
}
}
我希望检查分数字段的选项设置值,看看它是否与我定义的 ScoreMap 之一匹配。
SectionAPercentage = scoreMap[ScoreOptionB];
您没有检查
ScoreOptionB
不是null
。
SectionBScore = scoreMap[ScoreValueB];
您尚未检查
ScoreValueB
是否不是null
。
SectionCScore = scoreMap[ScoreValueC];
您尚未检查
ScoreValueC
是否不是null
。
看起来问题可能在于,即使从
FORM_CONTEXT.getAttribute()
返回的属性对象本身不是 null
,调用其 .getValue()
方法的结果也可能是——至少根据类型定义。
我假设,如果一个属性对象或其
.getValue()
结果是 null
,则应将其视为与其值为零一样。为 SectionAScore
等赋值应该可以使用:
var SectionAScore = scoreMap[ScoreOptionA?.getValue() ?? 0];
var SectionAPercentage = scoreMap[PercentageOptionA?.getValue() ?? 0];
var SectionBScore = scoreMap[ScoreOptionB?.getValue() ?? 0];
var SectionBPercentage = scoreMap[PercentageOptionB?.getValue() ?? 0];
var SectionCScore = scoreMap[ScoreOptionC?.getValue() ?? 0];
var SectionCPercentage = scoreMap[PercentageOptionC?.getValue() ?? 0];
这可以处理两种情况,因为:
null
,则 AttrObject?.getValue()
将解析为 null
。X ?? 0
的左侧是 null
,则结果将为 0
。