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();
}
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)?
– Leandro Duarte
I understood yes. I will use the callback to get the data of the teacher created and return to the screen. Thank you.
– Leandro Duarte
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; }); } ?
– Leandro Duarte
OK Marcelo. I had already noticed. I changed the code and it is working. I have another question, I will create another post. Thanks.
– Leandro Duarte