我在打字稿中有这个函数,我在其中为我收到的对象的属性赋值。
async buildFactsDocument(ruleId: string, updateDocument: UpdateObject) {
let facts: Array<string> = await this.getFacts(ruleId);
type FactKeys = keyof Fact;
await Promise.all(facts.map(async( fact: string )=>{
let value = await this.getFactsValues(ruleId, fact);
(((updateDocument as {update: Update}).update).fact![fact as FactKeys] as Values).old = value;
(((updateDocument as {update: Update}).update).fact![fact as FactKeys] as Values).new = value;
}));
return updateDocument;
};
有问题的对象是这个,我正在给里面的属性添加值:
let updateObject: UpdateObject = {
update: {
name: {
old: null,
new: null
},
priority: {
old: null,
new: null,
},
engine: {
old: null,
new: null
},
fact: {
brand: {
old: null,
new: null
},
model: {
old: null,
new: null
},
version: {
old: null,
new: null
},
year: {
old: null,
new: null
},
km: {
old: null,
new: null
},
color: {
old: null,
new: null
},
source: {
old: null,
new: null
},
},
adjustment: {
old: null,
new: null
}
},
create: {
conditions: null,
recipe: null
},
delete: {
conditions: null,
recipe: null
},
};
对象是在这个接口结构中声明的:
export interface UpdateObject {
update?: Update;
create?: CreateDelete;
delete?: CreateDelete;
}
export interface Update {
name?: Values;
priority?: Values;
engine?: Values;
fact?: Fact;
adjustment?: Values;
}
export interface Fact {
brand?: Values;
model?: Values;
version?: Values;
year?: Values;
km?: Values;
color?: Values;
source?: Values;
}
export interface Values {
old?: any;
new?: any;
}
export interface CreateDelete {
conditions?: any;
recipe?: any;
}
我有一个问题,在某些情况下,调用此函数时出现错误:无法设置未定义的属性“旧”。 this.getFacts 函数返回的事实是动态的,可能会改变。错误是因为这个吗?我没有找到错误的确切原因,因为在某些情况下它可以工作,而在其他情况下它会给出错误。