My JSON file has the following structure:
{
"user": [
{
"id": 0,
"data": [
{
"userName": "iheb",
"useremail": "",
"userPassword": "kkk"
}
],
"questionnaireListe": [
{
"questionnaire": [
{
"id": 2,
"section": [
{
"sectionName": "produit 1",
"question": [
{
"questionName": "question 1",
"answer": [
{
"answerName": "reponse 1"
}
]
}
]
},
{
"sectionName": "produit 2",
"question": [
{
"questionName": "question 2",
"answer": [
{
"answerName": "reponse 1"
},
{
"answerName": "reponse 2"
}
]
}
]
}
]
}
]
}
]
}
}
I have a service that includes a function to fetch data from this JSON file:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class QuestionnaireService {
constructor(private _Http:HttpClient) { }
getUser() {
return this._Http.get("http://localhost:3000/user");
}
}
In addition, I have a component.ts file structured like this:
import { Component, OnInit } from '@angular/core';
import { QuestionnaireService } from '../questionnaire.service';
...
// Content of UserComponent and respective functions here
...
Lastly, there is a separate service defined as follows:
import { Injectable, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { QuestionnaireService } from './questionnaire.service';
...
// Content of AuthUserService including login logic here
...
Despite using the same method in both the component and service files, the service throws an error:
ERROR TypeError: Cannot read property 'data' of undefined
If you can identify why the function works in the component but not in the service, any assistance would be greatly appreciated.