Get object that was created on the server via Angular

Asked

Viewed 93 times

0

Good morning Personal,

I am a beginner in Angular 4 (using Typescript) and I have a basic question, but I did not find an answer here.

I have a typescript method that calls a method from a Webapi. This Webapi method creates an object and returns OK(objectCreated). I’m unable to recover the data from the object that had just been created. I would like to give a console.log of the object I created, but the first time saved, it gives Undefined and the second time, it brings the previously created object and not the last.

I know it’s a rookie mistake in technology, I’m sorry.

Follows my code:

Webapi Controller:

    [HttpPost]
    public Professor Create(Professor p)
    {
        //if (!ModelState.IsValid)
        //{
        //    BadRequest();
        //}
        IKernel ninjectKernel = new StandardKernel();
        ninjectKernel.Bind<IProfessorBLO>().To<ProfessorBLO>();
        IProfessorBLO blo = ninjectKernel.Get<IProfessorBLO>();

        blo.Add(p);

        //return Created(new Uri(Request.RequestUri + "/" + p.Id), p);
        return p;
    }

Method in Typescript:

  submit(form){
    let professor = form.value.professor;
    let response = this.service.create(form.value.professor);
    console.log(response);
  }

Method this.service.create:

  create(object){
    this.http.post(this.url,JSON.stringify(object), this.options)
    .subscribe(response=> {
      this.response = response;
    });
    return this.response.json();
  }

1 answer

0


See that this.http.post is asynchronous, so that this.response may not have the value returned by the server when the function create end. A possible solution would be to pass a function of callback, to be executed when the response coming from the server reaches the client.

Method in Typescript:

submit(form){
  let professor = form.value.professor;
  this.service.create(form.value.professor,
    response=> console.log(response));
}

Method this.service.create:

create(object, callback){
  this.http.post(this.url,JSON.stringify(object), this.options)
  .subscribe(response=> {
    this.response = response;
    callback(response);
  });
}
  • Dear Marcelo, I had already weighed that the post is asynchronous, but I had no idea how to make him "wait" to get the ID. How would I use this callback function (I don’t know yet)?

  • I understood yes. I will use the callback to get the data of the teacher created and return to the screen. Thank you.

  • Marcelo, I still have a question. I, after getting the data back from the server, I have how to send them to the screen? Example: Submit(form){ Let Response = this.service.create(form.value.professor, (Response)=> { this.professor = Response; }); } ?

  • OK Marcelo. I had already noticed. I changed the code and it is working. I have another question, I will create another post. Thanks.

Browser other questions tagged

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