Reply POST comes empty

Asked

Viewed 117 times

0

I am making a POST request in my API by my angular project and only empty the result, and by Postman comes the correct data.

service ts. :

teste(user) {    

    return this.http.post(`api/home`, user)
  }

test.componentts.:

ok(user){           
    this.loginServ.teste(user)
      .subscribe(data => {
        let retorno = (data as any)
        console.log(retorno);
      })
  }

Return only comes empty and I can not understand the pq of this. Anyone can help?

  • why not use . then() instead of . subscribe()?

  • continues with the problem using the . then()

2 answers

1

I believe you’re returning a Promise, then the method should be used .then() in the request, the method .subscribe() is for Observables.

Service

function teste(user) {    

return this.http.post(`api/home`, user);

}

To Promises

ok(user){           
    this.loginServ.teste(user)
      .then(success);

    function success(response){
        console.log(response);
    }
}

For Observables

ok(user){           
    this.loginServ.teste(user)
      .subscribe(data => {
       console.log(data);
      });
  }

Promises vs Observables

Promises - Promises deal with sigular events when an asynchronous operation succeeds or fails.

Observable - An Observable is like a Stream and allows you to pass 0 or more events when Response is returned and also call functions or callbacks for these events.

  • does not work. 'cannot name Success'

  • @Mary edited.

  • continues to give error. 'Property 'then' does not exist on type 'Observable<Object>'

  • perfect, if it is a Observable then I will make an edition and mention you again in the comment. Next time you ask, try to provide more code or more context for the question, because it makes it harder to help and it just takes longer to help you solve your problem. ;)

  • Ah, yes. tendi. Thank you very much

  • @Maria see if the following code helps you, this can be a scope problem, if it is confirmed I can explain to you in chat what may have occurred

  • Keep coming back empty

  • @Maria have you debuggou to confirm if the problem is at the same angle? The return may be coming empty already from the backend

  • I debuggied and apparently everything is correct, but the return keeps coming empty, as if something was being sent wrong

  • I don’t know if this can be something, but when debbuguei now gave error 'Exception has occurred: number', 'Exception has occurred: Syntaxerror Syntaxerror: Unexpected number in JSON at position 1', 'Exception has occurred: Syntaxerror'

Show 5 more comments

1


in place of:

teste(user) {    

    return this.http.post(`api/home`, user)
  }

I resolved so:

teste(user) {    

    return this.http.post(`api/home`,  {user:user})
  }
  • 1

    after a thousand attempts, it turns out that the problem is solved with a simple solution like this, very good +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.