Jonathan
Jonathan

Reputation: 225

UnhandledPromiseRejectionWarning TypeError: Cannot read property 'property' of undefined

I have the following code, it tries to read a txt, convert it to an array and then convert it to json, to save it in a db, but when calling the controller, I try to run the SaveReferences function, it sends me this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'service' of undefined

Also, send me this: Promise { TypeError: Cannot read property 'service' of undefined at GettingInfo.SaveReferences at GettingInfo.Readfile at ReferenceController.saved

  export interface Dataconf{
        service:number;
        name:string;
        ref:string
    }
    
    export class GettingInfo {
    
        constructor(private referenceService: ReferenceService) {
        }
    
        Readfile = () => {
    
            const file = path.resolve(__dirname, '../../../dist/entity/PRUEBA.txt')
            try {
                const data = fs.readFileSync(file, 'utf-8');
                const lines = data.split("\n")
                let values = []
                let bi = []
                lines.forEach(line => {
                    line.trim()
                    values = line.split("\|", 6).map(a => a.trim());
                    bi.push(values)
                    console.log(bi)
     })
                const convert = this.ConditionData(bi)
                console.log(convert)
                const save = this.SaveReferences(convert)
                console.log(save)
    
            } catch (err) {
                console.error(err), "something has happened to the file";
            }
        }
    
        ConditionData(values): Array<Dataconf> {
            let resultado = [];
            values.forEach(arreglo => {
                let ref = 'ref';
                for (let i = 3; i < arreglo.length; i++) {
                    if (arreglo[i].length > 0) {
                        let obj = {
                            service: parseInt(arreglo[0]),
                            name: arreglo[1]
                        }
                        obj[ref] = arreglo[i];
                        resultado.push(obj);
                    }
                }
            });
            console.log("resultado funcionConditionData", resultado)
            return resultado;
        }

async SaveReferences(data: Array<Dataconf>) {
        console.log("array", data)
        let i
        let orderField = 0;
        let helper = data[i].service;
        for (i = 0; i <= data.length; i++) {
            if (data[i].service != helper) {
                helper = data[i].service;
                orderField = 0
                try {
                    let res = await this.referenceService.createReference({
                        service: data[i].service,
                        name: `ref${i}`,
                        label: data[i].ref,
                        longitud: 0,
                        order: orderField
                    });
                } catch (e) {
                    console.error(e);
                }
            }
        }
        return data;
    }

Upvotes: 0

Views: 2226

Answers (2)

wak786
wak786

Reputation: 1615

**let i
let orderField = 0;
let helper = data[i].service;**

This block of code is causing error. You declared i but did not initialize it. So it's default value is undefined. And then you are trying to do data[i].service which translates to data[undefined].service

Upvotes: 0

Ravi
Ravi

Reputation: 2281

In SaveReferences 4th line i is undefined

let helper = data[i].service;

It should be

let helper;

As helper is assigned inside the for loop

Upvotes: 1

Related Questions