My goal is to access the "rename_fields" object within the main_object collection in order to utilize its field values:
export interface StdMap<T = string> {
[key: string]: T;
}
export type StdFileBasedPluginHandlerConfiguration<
SourceType extends StdMap<string | number>
> = {
type: "std_file";
data_files: string[];
exclude_fields: string[];
value_fields: string[];
rename_fields: StdMap<StdMap<string>>;
descriptor: string;
};
export type PluginHandlerConfiguration =
| StdFileBasedPluginHandlerConfiguration<any>
| { type: "not_required_configuration" }
| undefined;
// export type PluginHandlerConfiguration = StdFileBasedPluginHandlerConfiguration<
// any
// >;
export interface CollectorConfiguration {
lastUpdate: Date;
hashFile: string;
code: string;
plugin_path?: string;
plugin_configuration: PluginHandlerConfiguration;
related_codes?: string[];
collections: { original: string; names: string[] };
skipCollectData?: boolean;
skipFlatGeneration?: boolean;
extra_grouping_fields: string[];
has_origins: boolean;
force?: boolean;
notify?: boolean;
origins_that_generate_extra_records?: string[];
}
const main_object: CollectorConfiguration=
{
"code" : "my_code",
"lastUpdate" : new Date("2020-01-28T00:00:00.000+0000"),
"collections" : {
"original" : "collection",
"names" : [
"collection_1",
"collection_2"
]
},
"hashFile" : "ffc0b10ac2e7cd681f5666a474063165f5507212c45abf4ee2f85482ea866985,13c1dd232e13bc6d20ffe6213f38c5152e1f5e7f72366b461602d3cd876ef40f",
"extra_grouping_fields" : [
"type"
],
"has_origins" : true,
"plugin_path" : "file/path_to_plugin",
"plugin_configuration" : {
"type" : "std_file",
"data_files" : [
"../file1.csv",
"../file2.csv"
],
"value_fields" : [
"value1",
"value2"
],
"descriptor" : "type",
"exclude_fields" : [
"excl1",
"excl2"
],
"rename_fields" : {
"flat" : {
"TEST1" : "test1",
"TEST2" : "test2",
}
}
}
}
Object.keys(main_object).forEach((key: Date | string | StdFileBasedPluginHandlerConfiguration<any> | boolean)=>{
console.log(`KEY: ${key} - typeof key: ${typeof key}`);
Object.keys(main_object).forEach((keyConfiguration) => {
console.log(`DEBUG-->configuration keys: ${keyConfiguration}`);
if (keyConfiguration === "plugin_configuration") {
Object.keys(main_object[keyConfiguration]!).forEach(
(keyPluginConfiguration: any) => {
console.log(
`DEBUG-->plugin_configuration - ${keyPluginConfiguration} --- END plugin_configuration`
);
if (keyPluginConfiguration === "rename_fields") {
Object.keys(
keyConfiguration![keyPluginConfiguration]!
).forEach((keyRenameFields: any) => {
console.log(
`DEBUG-->rename_fields - ${keyRenameFields} --- END rename_fields`
);
});
}
}
);
}
});
});
However, I encountered an error message:
test_object_loop.js:50
Object.keys(keyConfiguration[keyPluginConfiguration]).forEach(function (keyRenameFields) {
^
TypeError: Cannot convert undefined or null to object
at Function.keys ()
at test_object_loop.js:50:28
at Array.forEach ()
at test_object_loop.js:47:56
at Array.forEach ()
at test_object_loop.js:44:30
at Array.forEach ()
at Object. (test_object_loop.js:42:26)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
Is there a way for me to access an object nested inside another object with a specific type?