Working on a post method in an Angular 4 web app, I am able to save data to the database successfully. However, I am facing an issue where an ID (return value) is passed from the DB to the API for Angular as another function parameter. How can I retrieve that value in my Angular code and use it as the next function parameter? I am new to Angular, so please excuse any mistakes in my question. (I need to extract the TransactionId value from the API to Angular)
Below is my TypeScript (TS file):
onClick() {
this.header = new IHeaderstock(this.userid, this.created, this.CompanyID,
this.modified, this.modifieduserid, this.confirm, this.shopid);
this.headeritems.push(this.header);
this._enqService.CatchHeaderDetail(this.headeritems)
.subscribe(value => {
if (typeof value !== 'undefined' && value != null) {
value.forEach(header => {
this.headeritems.push(this.header)
});
}
},
error => {
console.error(error);
this.statusMessage = "Problem with the service. Please try again after some time";
});
additem{
this.items = new IStockitems(this.shopid,value.ItemCode, value.ItemDescription, value.PackingtypeName, value.Stock, this.TransactionId );
} //here this.TransactionId is the returned value from the above post method.
Next is my service file:
CatchHeaderDetail(stock: any)
: Observable<IHeaderstock[]> {
let stockheaderdetail = stock;
console.log(stockheaderdetail)
debugger;
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:3071/api/Stockcountheader/' + 'Stock', stockheaderdetail, options)
.map((response: Response) => <IHeaderstock[]>response.json())
.catch(this.handleError);
}
Lastly, my WebAPI implementation (please note that the API returns the value successfully):
My API implementation (working fine)
public class StockcountheaderController : ApiController
{
private adminv2Entities enqentities = new adminv2Entities();
[HttpPost]
private IActionResult Stock([FromBody] List<spGetNewStockCountHeader_Result>
jsonvalues)
{
ObjectParameter TransactionId = new ObjectParameter("TransactionId", typeof(Int32));
foreach (spGetNewStockCountHeader_Result Datastock in jsonvalues)
{
spGetNewStockCountHeader_Result Stockobject = new spGetNewStockCountHeader_Result();
Stockobject.UserID = Datastock.UserID;
Stockobject.created = Datastock.created;
Stockobject.CompanyID = Datastock.CompanyID;
Stockobject.modified = Datastock.modified;
Stockobject.modifieduserid = Datastock.modifieduserid;
Stockobject.confirm = Datastock.confirm;
Stockobject.ShopId = Datastock.ShopId;
enqentities.spGetNewStockCountHeader(Datastock.UserID, Datastock.created, Datastock.CompanyID, Datastock.modified, Datastock.modifieduserid, Datastock.confirm,Datastock.ShopId, TransactionId);
}
return Ok(new { data = TransactionId.Value});
}