3

I want to display individual value of json object into form in angular. How to do that?

output

{
  "Order": {
    "active": true,
    "Orders_Entrydate": "2017-12-31T18:30:00.000Z",
    "LastTouchDateTime": "2018-05-10T05:46:38.702Z",
    "_id": "5af07544eb26f918e0e2ff74",
    "Orders_Name": "Test1",
    "Orders_Number": "1011",
    "Orders_LoanNumber": 1328,
    "Orders_PropertyAddress1": "test address1",
    "Orders_PropertyAddress2": "test address 1",
    "Orders_PropertyCity": "testCity",
    "Orders_Propertyzipecode": 1236,
    "Orders_Countyfee": 500,
    "Orders_Additionalfee": 100,
    "Orders_Customerpricing": 150
  }
}

view-order.ts

export class countyViewOrderComponent implements OnInit {
  orders: any[];

  constructor(private orderService: countyvieworder, private router: Router, private cookie_service: CookieService) {

  }
  getorder() {
    this.orderService.getOrders().subscribe(response => {
      this.orders = response.json();


    })
  }


  onSelect(selectedItem: any) {
    this.cookie_service.put('key', selectedItem._id)
    // this.cookie_service.get('key')
    this.router.navigate(['pages/home/County/orderEntrybyCounty']);


  }



  ngOnInit() {

    this.getorder()


  }


}

getOrderbyOrderNo() service

getOrderbyOrderNo() {
        this.id = this.cookie_service.get('key');
        return this.http.get('http://localhost:8080/View/'+ this.id)

    }

Getting this error:

Argument of type 'Response' is not assignable to parameter of type 'string'.

7
  • Please share orderService.getOrderbyOrderNo() code as well. There is a type mismatch. What is the type of the response being returned by getOrderByOrderNo? Commented May 10, 2018 at 6:10
  • getOrderbyOrderNo() { this.id = this.cookie_service.get('key'); return this.http.get('localhost:8080/View/'+ this.id) } Commented May 10, 2018 at 6:16
  • Please post the whole function, including the return type, in the original post, not as a comment. Commented May 10, 2018 at 6:16
  • what is your this.orders Commented May 10, 2018 at 6:17
  • @rina are you using HttpModule or HttpClientModule Commented May 10, 2018 at 6:44

3 Answers 3

1

Suggest you to make use of HttpClient , that will do conversation for you . HTTPClient is new module in angular which replaced old HTTP one.

import HttpClientmodule in your module

import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [
    HttpClientModule,
  ],

change in service.ts

   import { HttpClient } from '@angular/common/http';
  constructor(private httpClient: HttpClient) { }

  getOrderbyOrderNo() :Observable<any> {
        this.id = this.cookie_service.get('key');
        return this.httpClient.get('http://localhost:8080/View/'+ this.id)
    } 

view-order.ts

 order: any;//no array as you are expecting only one element 
 getorder() {
    this.orderService.getOrders().subscribe(response => {
      this.order = response;
    })
  }

in your code you are using array that is also cause problem

Sign up to request clarification or add additional context in comments.

Comments

1

Change the getorder function subscribe to subscribe((response: any))

getorder() {
    this.orderService.getOrderbyOrderNo().subscribe((response: any) => {    
         console.log(response);          
        this.orders=response

    })
}

3 Comments

yes he might be using that, as generally everyone is using that.
but doesnt look like as its named http , and doing converstation which is not needed
yeah, http is variable name, but we dont know what he is using since he didnt mentioned anything
0

You are not parsing your response so here this code may be help you

 this.orders=JSON.parse(response);

or you can also do this in your service

return this.http.get('http://localhost:8080/View/'+ this.id).map(data => data.json());

9 Comments

@vikas it will works fine if you use HttpClientModule
yes i'm using httpclientModule . import {Http, RequestOptions, Headers} from "@angular/http"; constructor(private _http: Http) { }
@Vikas i tried and its working in my machine and if you have any query with your code then share me so i can try it on my machine and help you to find solution
@rajan i am sure that you have first define HttpModule and then after override HttpClientModule, is it right ????
I don't have any query all I wanted to convey is HttpModule is deprecated if OP is using Angular version> 4.3 then he should be using HttpClientModue which by default formats the response to JSON so you do not require resposne.json()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.