最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Angular 2 Property subscribe does not exist on type Promise <any> - Stack Overflow

matteradmin8PV0评论

I have imported 'rxjs/add/operator/toPromise';

In my Ts file i have something like:

onChange(categoryid) {
//console.log('testing')
this.productService.getProductsOncategory(categoryid).subscribe(data => {
  if (data.success) {

    console.log('Products obtained');
  } else {
    console.log('Not obtained!');
  }
 });
}

I get error:

Property 'subscribe' does not exist on type 'Promise'.

getProductsOncategory(category_id) {

let catUrl = "API URL"
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let catIdObj = JSON.stringify({ category_id: category_id })
return this.http.post(catUrl, catIdObj, { headers: headers })
  .toPromise()
  .then((response: Response) => response.json().data)
  //.map((response: Response) => response.json())
  //.do(data => console.log(JSON.stringify(data)))
  .catch(this.handleError);

}

How to i change the snippet if i have to use ' then(data => '...

I have imported 'rxjs/add/operator/toPromise';

In my Ts file i have something like:

onChange(categoryid) {
//console.log('testing')
this.productService.getProductsOncategory(categoryid).subscribe(data => {
  if (data.success) {

    console.log('Products obtained');
  } else {
    console.log('Not obtained!');
  }
 });
}

I get error:

Property 'subscribe' does not exist on type 'Promise'.

getProductsOncategory(category_id) {

let catUrl = "API URL"
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let catIdObj = JSON.stringify({ category_id: category_id })
return this.http.post(catUrl, catIdObj, { headers: headers })
  .toPromise()
  .then((response: Response) => response.json().data)
  //.map((response: Response) => response.json())
  //.do(data => console.log(JSON.stringify(data)))
  .catch(this.handleError);

}

How to i change the snippet if i have to use ' then(data => '...

Share Improve this question edited Jul 20, 2017 at 9:54 Zooly 4,7974 gold badges39 silver badges55 bronze badges asked Jul 20, 2017 at 9:21 phymephyme 3513 gold badges11 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

subscribe method is for 'Observable', so you can just return an Observable Object. then do what you want to do like this

getProductsOncategory(category_id): Observable<Response>{

        let catUrl = "API URL"
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        let catIdObj = JSON.stringify({ category_id: category_id })
        return this.http.post(catUrl, catIdObj, { headers: headers })
  }

this.productService.getProductsOncategory(categoryid)
    .map(response => response.json().data)
    .subscribe(data => doSomething(data),err => catchErr(err));

just map the http request and return it. no need to create a new promise

getProductsOncategory(category_id) {

    let catUrl = "API URL"
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let catIdObj = JSON.stringify({
        category_id: category_id
    })

    return this.http.post(catUrl, catIdObj, {
            headers: headers
        })
        .map((res: Response) => res.json());
}
Post a comment

comment list (0)

  1. No comments so far