When attempting to retrieve an object from the server using http.get, I am encountering an issue where the strings remain undefined during the first iteration. View the full object
The integers are functioning correctly, but there is a problem with the strings.
this.http.get("/Home/getRoom", { headers: headers }).map(res => res.json()).subscribe(q => {
this.timecount = q.timeperquestion;
this.player1id = JSON.stringify(q.player1id);
this.player2id = q.player2id;
}, err => console.error(err),
() => console.log('Complete'));
After the first iteration of the function, the strings are displayed properly.
EDIT:
This is the class:
class AppComponent implements AfterViewInit {
public answered = false;
public title = "loading question...";
public options = [];
public correctAnswer = false;
public working = false;
public timecount=15;
public timer;
public roomID;
public player1id;
public player2id;
constructor(@Inject(Http) private http: Http, private Ref: changeDetectorRef) {
}
nextQuestion() {
this.working = true;
this.answered = false;
this.title = "loading question...";
this.options = [];
var headers = new Headers();
headers.append('If-Modified-Since', 'Mon, 27 Mar 1972 00:00:00 GMT');
this.http.get("/Home/getRoom", { headers: headers }).map(res => res.json()).subscribe(q => {
this.timecount = q.timeperquestion;
this.player1id = JSON.stringify(q.player1id);
this.Ref.detectChanges();
},
err => console.error(err),
() => console.log('Complete'));
EDIT 2: It appears that everything is functioning correctly with the strings. When attempting to display them using {{player1id}}, the correct value is shown even during the first iteration. The slight "problem" lies in using console.log(player1id) to check the string, as it only displays the correct value after one iteration. Thank you for your assistance!