As a newcomer to Angular and Typescript, I am facing an issue with passing a reference into the subscribe function. In my code snippet below, I am attempting to assign the user value from the observable, but it seems like the function is not recognizing the reference. I have looked at other similar questions on Stack Overflow, but I am unsure how to convert this into a pure arrow function or obtain an instance of ProfileComponent to successfully assign the value.
I admit that I am still learning, so any guidance on how to handle this situation would be greatly appreciated. Is there an alternative to observables? For instance, if you only need to fetch one item from a GET method, wouldn't it be unnecessary to use an observable?
Please assist me in resolving this issue.
import { Component, OnInit } from '@angular/core';
import { UserService } from '../_services';
import { User } from '../_models';
import { userInfo } from 'os';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
user: User;
constructor(userService: UserService) {
this.user = new User();
const obs$ = userService.getAll().subscribe({
next(val) {
console.log('Current object: ', val, ' user ', this.user);
this.user.userId = val[0].userId;
this.user.username = val[0].username;
},
error(msg) { console.log('Error Object: ', msg); }
}
);
/*
this.user.userId = obs[0].id;
this.user.username = obs[0].username;
this.user.password = obs[0].password;
*/
}
ngOnInit() {
}
}