I've encountered a problem with the following code snippet in my function:
let packName: string = respPack.find(a => {a.id == 'name_input'}).answer.replace(/ /,'_');
My goal is to locate an object by matching its id and retrieve the value stored in its answer property. However, I keep receiving an error message stating:
cannot read property answer of undefined.
I am unsure if I'm going about this the correct way. To provide more context, here is the remainder of my function for better understanding:
saveResponses(){
const respPack = this.ResponseList;
const sendTarget: FirebaseObjectObservable<any> = this.afdb.object('/submissions');
let dataLoad:{ [prop : string]: Array<any> } = {};
let packName: string = respPack.find(a => {a.id == 'name_input'}).answer.replace(/ /,'_');
respPack.forEach( a => {
if(a.answer){
let data = { question: a.question, answer: a.answer, id: a.id };
dataLoad[packName].push(data);
}
else if(a.responses){
let dataChunk = { question: a.question, id: a.id, responses: Array<any> };
a.responses.forEach(resp => {
let respChunk = { response: resp.response, value: resp.value, id: resp.id };
dataChunk.responses.push(respChunk);
});
dataLoad[packName].push(dataChunk);
}
});
sendTarget.set(dataLoad);
}