Ionic2 / Angular2 MailChimp API GET response

跟風遠走 提交于 2019-12-24 03:23:58

问题


Okey so I've got a little problem with MailChimp response. So here is the thing.

I want to check the status of the subscribed user. I have the PHP code which is works fine and i have the code which is also works fine so I get the response BUT I can't use the response after it. So here is the codes:

I have a MailService provider which contain this function:

postCheck(post: {email: string}): Observable<any>{
    const email = JSON.stringify(post);
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-urlencoded');
    return this.http.post('http://localhost:8100/getapi', email, {
      headers: headers
    }).map(res => {
          console.log(res.json())
          return res.json(); //add this line
       });
  }

In the main page I have this function:

sendCheck(email: string){
      this.mailservice.postCheck({email: email})
      .subscribe(
        response => this.response = response.status,
        error => console.log(error)
      );
  }

In the main page html when i call <p>{{ response }}</p> it write out 'pending' or 'subscribed'. But after it when I try console.log(this.response); it write out nothing so in the code I can't really do the checking.


回答1:


From what I can gather, you want to do something with your response after the data has arrived. This you need to do inside the subscription, to ensure that the data is available. So something like this:

sendCheck(email: string){
  this.mailservice.postCheck({email: email})
  .subscribe(response => { 
     this.response = response.status;
     // here the value has been set to response, so do what you want
     this.doSomethingWithResponse();
  )};
}

doSomethingWithResponse() {
  if(this.response == 'subscribed') {
     // do stuff
  }
}


来源:https://stackoverflow.com/questions/42830778/ionic2-angular2-mailchimp-api-get-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!