I'm not completely clear on what you are trying to do ... but you probably need that code within the .map method. Like this:
export class UserComponent implements OnInit {
existingUser: any = {};
ngOnInit() {
this.activatedRoute.params.subscribe((params: Params) => {
this.id = params['id'];
console.log(this.id);
this.db.object(`/users/${this.id}`).map(value => {
console.log(value);
this.existingUser = value;
console.log(this.existingUser);
this.userForm = this.formBuilder.group({
first_name: [this.existingUser.first_name, Validators.required],
})
})
});
};
}
Or put that code in a method that is called from here.
UPDATE: Here is an example from my application that does something similar:
ngOnInit(): void {
this.productForm = this.fb.group({
productName: ['', [Validators.required,
Validators.minLength(3),
Validators.maxLength(50)]],
productCode: ['', Validators.required],
starRating: ['', NumberValidators.range(1, 5)],
tags: this.fb.array([]),
description: ''
});
// Read the product Id from the route parameter
this.route.params.subscribe(
params => {
let id = +params['id'];
this.getProduct(id);
}
);
}
getProduct(id: number): void {
this.productService.getProduct(id)
.subscribe(
(product: IProduct) => this.onProductRetrieved(product),
(error: any) => this.errorMessage = <any>error
);
}
onProductRetrieved(product: IProduct): void {
if (this.productForm) {
this.productForm.reset();
}
this.product = product;
if (this.product.id === 0) {
this.pageTitle = 'Add Product';
} else {
this.pageTitle = `Edit Product: ${this.product.productName}`;
}
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
this.productForm.setControl('tags', this.fb.array(this.product.tags || []));
}
You can find the full set of code here: https://github.com/DeborahK/Angular2-ReactiveForms (in the APM folder)