How do I take datatype of any in Angular and convert to known data type?
I have a service below, receiving some service data, and want to convert to productDataClass which has {productId: int; productName: string;}
At the very least, I at least want productId
which is an integer.
Sending data similar to this resource,
public productDataClass: ProductDataClass;
this.productService.currentMessage.subscribe(currentMessage => {
this.productDataClass = currentMessage
I then tried this, which is not working copyFromMessage is any
type,
this.productService.currentMessage.subscribe(currentMessage => {
this.copyFromMessage = currentMessage;
this.productData = this.copyFromMessage.value;
this.testString= this.productData.productName;
Error:
Cannot read property 'productName' of undefined
Basic Service:
export class ProductService{
private messageSource = new Subject();
currentMessage = this.messageSource.asObservable();
constructor() { }
changeMessage(currentMessage) {
this.messageSource.next(currentMessage);
}
}
Looking in this resource also: Observable type error: cannot read property of undefined
Screenshot of actual items:
CurrentMessage has actual data, following lines show undefined,
['value'] is still giving error