Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未处理的PromisejectionWarning TypeError:无法读取属性';财产';未定义的_Javascript_Node.js_Typescript - Fatal编程技术网

Javascript 未处理的PromisejectionWarning TypeError:无法读取属性';财产';未定义的

Javascript 未处理的PromisejectionWarning TypeError:无法读取属性';财产';未定义的,javascript,node.js,typescript,Javascript,Node.js,Typescript,我有以下代码,它尝试读取txt,将其转换为数组,然后将其转换为json,将其保存在db中,但在调用控制器时,我尝试运行SaveReferences函数,它向我发送以下错误: 未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“service” let helper = data[i].service; 还有,给我这个:答应我{ TypeError:无法读取在GettingInfo.SaveReferences处未定义的属性“service”,在Gett

我有以下代码,它尝试读取txt,将其转换为数组,然后将其转换为json,将其保存在db中,但在调用控制器时,我尝试运行SaveReferences函数,它向我发送以下错误: 未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“service”

let helper = data[i].service;
还有,给我这个:答应我{ TypeError:无法读取在GettingInfo.SaveReferences处未定义的属性“service”,在GettingInfo.Readfile处位于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;
    }
导出接口数据配置{
服务:电话号码;
名称:字符串;
参考:字符串
}
导出类获取信息{
构造函数(私有引用服务:引用服务){
}
Readfile=()=>{
const file=path.resolve(_dirname,'../../../dist/entity/PRUEBA.txt'))
试一试{
const data=fs.readFileSync(文件“utf-8”);
常量行=数据分割(“\n”)
让值=[]
设bi=[]
lines.forEach(line=>{
行。trim()
values=line.split(“\\\”,6).map(a=>a.trim());
推送(值)
控制台日志(bi)
})
const convert=this.ConditionData(bi)
console.log(转换)
const save=this.SaveReferences(转换)
console.log(保存)
}捕捉(错误){
错误(err),“文件发生了一些问题”;
}
}
条件数据(值):数组{
设resultado=[];
values.forEach(arreglo=>{
设ref='ref';
for(设i=3;i0){
设obj={
服务:parseInt(arreglo[0]),
姓名:arreglo[1]
}
obj[ref]=arreglo[i];
结果推挤(obj);
}
}
});
log(“resultado functionconditiondata”,resultado)
返回resultado;
}
异步保存引用(数据:数组){
日志(“数组”,数据)
让我
设orderField=0;
设helper=data[i]。服务;

保存引用中的(i=0;i第4行
i
未定义

let helper = data[i].service;
应该是

let helper;
As辅助对象是在for循环中指定的

**let i
let orderField = 0;
let helper = data[i].service;**
此代码块正在导致错误。您声明了
i
,但未对其进行初始化。因此,它的默认值为
undefined
。然后您尝试执行
data[i]。服务
转换为
data[undefined]。服务