为什么变量在’.find’中始终返回未定义?

huangapple go评论70阅读模式
英文:

Why variable always returning undefined in the '.find'?

问题

我真的不知道为什么我的变量一直返回未定义。

所以问题基本上是'schoolAndGradeLevelId'始终返回未定义...我试图创建一个将学生信息推送到变量的函数,稍后此变量将用于将信息发送到我的API以在数据库中创建这些学生。如果有人可以帮助我,我会很感激!

public onFileSelected(files: Array<File>): void {
    if (files.length === 1) {
      this.file = files[0];
      this.loadFile();
      this.fileError = null;
    } else {
      this.file = null;
      this.fileError = 'Selecione apenas um arquivo';
    }
}

private fetchShcoolAndGradeLevelId(
    schoolName: string,
    gradeLevelName: string
): void {
    const query = this.queriesHandler.handle(
        new GetSchoolAndGradeLevelIdByNameQuery(schoolName, gradeLevelName)
    );
    query.subscribe({
        next: (res) => {
            this.schoolAndGradeLevelIds.push({
                ...res.data[0],
                schoolName,
                gradeLevelName,
            });
        },
    });
}

private loadFile(): void {
    let fileReader = new FileReader();
    fileReader.readAsBinaryString(this.file);
    fileReader.onload = (e) => {
        const excel = <string>fileReader.result;
        const rawStudents = parseImportedStudents(excel);
        rawStudents.forEach((student) => {
            let schoolAndGradeLevelId = this.schoolAndGradeLevelIds.find(
                (s) =>
                    s.schoolName == student.schoolName &&
                    s.gradeLevelName == student.gradeLevelName
            );
            if (!schoolAndGradeLevelId) {
                this.fetchShcoolAndGradeLevelId(
                    student.schoolName,
                    student.gradeLevelName
                );
                schoolAndGradeLevelId = this.schoolAndGradeLevelIds.find(
                    (s) =>
                        s.schoolName == student.schoolName &&
                        s.gradeLevelName == student.gradeLevelName
                );
            }
            this.students.push({
                studentName: student.studentName,
                gradeLevelName: student.gradeLevelName,
                schoolName: student.schoolName,
                documentNumber: student.documentNumber,
                birthDate: student.birthDate,
                schoolId: schoolAndGradeLevelId?.schoolId,
                gradeLevelId: schoolAndGradeLevelId?.gradeLevelId,
            });
        });
    };
}

public onSave(): void {
    const data: ICreateMultipleStudentsRequest = {
        students: this.students.map((student) => {
            return {
                studentName: student.studentName,
                gradeLevelName: student.gradeLevelName,
                schoolName: student.schoolName,
                schoolId: student.schoolId,
                gradeLevelId: student.gradeLevelId,
                documentNumber: student.documentNumber,
                birthDate: student.birthDate,
            };
        }),
    };
    const command = new CreateMultipleStudentsCommand(data);
    this.commandsHandler.handle(command).subscribe(
        () => {
            this.toast.showSuccess('Estudantes importados com sucesso');
            this.hideSpinner();
            this.dialog.hide();
            this.studentsImported.emit();
        },
        (err) => {
            this.toast.showError(err.error?.errorMessage);
            this.hideSpinner();
        }
    );
}

希望这有助于解决您的问题。

英文:

I really don't know why my variable is aways returning undefined.

So the problem is basically that the 'schoolAndGradeLevelId' always return undefined... I'm trying to do a function that push students information to a variable, and later this variable will be used to send the info to my API to create these students in the database. If someone can help me, I'd be grateful!

  public onFileSelected(files: Array&lt;File&gt;): void {
if (files.length === 1) {
this.file = files[0];
this.loadFile();
this.fileError = null;
} else {
this.file = null;
this.fileError = &#39;Selecione apenas um arquivo&#39;;
}
}
private fetchShcoolAndGradeLevelId(
schoolName: string,
gradeLevelName: string
): void {
const query = this.queriesHandler.handle(
new GetSchoolAndGradeLevelIdByNameQuery(schoolName, gradeLevelName)
);
query.subscribe({
next: (res) =&gt; {
this.schoolAndGradeLevelIds.push({
...res.data[0],
schoolName,
gradeLevelName,
});
},
});
}
private loadFile(): void {
let fileReader = new FileReader();
fileReader.readAsBinaryString(this.file);
fileReader.onload = (e) =&gt; {
const excel = &lt;string&gt;fileReader.result;
const rawStudents = parseImportedStudents(excel);
rawStudents.forEach((student) =&gt; {
let schoolAndGradeLevelId = this.schoolAndGradeLevelIds.find(
(s) =&gt;
s.schoolName == student.schoolName &amp;&amp;
s.gradeLevelName == student.gradeLevelName
);
if (!schoolAndGradeLevelId) {
this.fetchShcoolAndGradeLevelId(
student.schoolName,
student.gradeLevelName
);
schoolAndGradeLevelId = this.schoolAndGradeLevelIds.find(
(s) =&gt;
s.schoolName == student.schoolName &amp;&amp;
s.gradeLevelName == student.gradeLevelName
);
}
this.students.push({
studentName: student.studentName,
gradeLevelName: student.gradeLevelName,
schoolName: student.schoolName,
documentNumber: student.documentNumber,
birthDate: student.birthDate,
schoolId: schoolAndGradeLevelId?.schoolId,
gradeLevelId: schoolAndGradeLevelId?.gradeLevelId,
});
});
};
}
public onSave(): void {
const data: ICreateMultipleStudentsRequest = {
students: this.students.map((student) =&gt; {
return {
studentName: student.studentName,
gradeLevelName: student.gradeLevelName,
schoolName: student.schoolName,
schoolId: student.schoolId,
gradeLevelId: student.gradeLevelId,
documentNumber: student.documentNumber,
birthDate: student.birthDate,
};
}),
};
const command = new CreateMultipleStudentsCommand(data);
this.commandsHandler.handle(command).subscribe(
() =&gt; {
this.toast.showSuccess(&#39;Estudantes importados com sucesso&#39;);
this.hideSpinner();
this.dialog.hide();
this.studentsImported.emit();
},
(err) =&gt; {
this.toast.showError(err.error?.errorMessage);
this.hideSpinner();
}
);
}

答案1

得分: 0

以下是代码部分的翻译:

// 定义了一个名为 `loadFile` 的公共方法
public loadFile(): void {
  // 创建一个文件读取器对象
  let fileReader = new FileReader();
  // 以二进制字符串格式读取文件内容
  fileReader.readAsBinaryString(this.file);
  // 当文件读取完成时执行以下操作
  fileReader.onload = (e) => {
    // 获取读取的文件内容并转换为字符串
    const excel = <string>fileReader.result;
    // 解析导入的学生数据
    const rawStudents = parseImportedStudents(excel);
    // 遍历解析后的学生数据
    rawStudents.forEach((student) => {
      // 将学生数据添加到学生数组中
      this.students.push({
        studentName: student.studentName,
        gradeLevelName: student.gradeLevelName,
        schoolName: student.schoolName,
        documentNumber: student.documentNumber,
        birthDate: student.birthDate,
      });
    });
  };
  // 当文件读取结束时执行以下操作
  fileReader.onloadend = (s) => {
    // 遍历学生数组
    this.students.forEach((student) => {
      // 查找匹配学校名称和年级名称的学校和年级等级 ID
      let schoolAndGradeLevelId = this.schoolAndGradeLevelIds.find(
        (s) =>
          s.schoolName == student.schoolName &&
          s.gradeLevelName == student.gradeLevelName
      );
      // 如果找不到匹配的学校和年级等级 ID
      if (!schoolAndGradeLevelId) {
        // 创建一个查询以获取学校和年级等级 ID
        const query = this.queriesHandler.handle(
          new GetSchoolAndGradeLevelIdByNameQuery(
            student.schoolName,
            student.gradeLevelName
          )
        );
        // 订阅查询结果
        query.subscribe({
          next: (res) => {
            // 将获取的学校和年级等级 ID 添加到数组中
            this.schoolAndGradeLevelIds.push({
              ...res.data[0],
              schoolName: student.schoolName,
              gradeLevelName: student.gradeLevelName,
            });
            student.schoolId = res.data[0]?.schoolId;
            student.gradeLevelId = res.data[0]?.gradeLevelId;
          },
        });
      } else {
        // 如果找到匹配的学校和年级等级 ID,直接赋值给学生对象
        student.schoolId = schoolAndGradeLevelId?.schoolId;
        student.gradeLevelId = schoolAndGradeLevelId?.gradeLevelId;
      }
    });
  };
}

希望这个翻译对您有所帮助。如果有其他问题或需要进一步的帮助,请随时告诉我。

英文:

So what i did to solve this problem was basically this:

public loadFile(): void {
let fileReader = new FileReader();
fileReader.readAsBinaryString(this.file);
fileReader.onload = (e) =&gt; {
const excel = &lt;string&gt;fileReader.result;
const rawStudents = parseImportedStudents(excel);
rawStudents.forEach((student) =&gt; {
this.students.push({
studentName: student.studentName,
gradeLevelName: student.gradeLevelName,
schoolName: student.schoolName,
documentNumber: student.documentNumber,
birthDate: student.birthDate,
});
});
};
fileReader.onloadend = (s) =&gt; {
this.students.forEach((student) =&gt; {
let schoolAndGradeLevelId = this.schoolAndGradeLevelIds.find(
(s) =&gt;
s.schoolName == student.schoolName &amp;&amp;
s.gradeLevelName == student.gradeLevelName
);
if (!schoolAndGradeLevelId) {
const query = this.queriesHandler.handle(
new GetSchoolAndGradeLevelIdByNameQuery(
student.schoolName,
student.gradeLevelName
)
);
query.subscribe({
next: (res) =&gt; {
this.schoolAndGradeLevelIds.push({
...res.data[0],
schoolName: student.schoolName,
gradeLevelName: student.gradeLevelName,
});
student.schoolId = res.data[0]?.schoolId;
student.gradeLevelId = res.data[0]?.gradeLevelId;    
},
});
} else {
student.schoolId = schoolAndGradeLevelId?.schoolId;
student.gradeLevelId = schoolAndGradeLevelId?.gradeLevelId;
}
});
};

}

huangapple
  • 本文由 发表于 2023年2月27日 07:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定