I have this in my server to fetch data from a database:
[HttpPost]
[Route("api/getaddress")]
public IQueryable<PreAddress> GetAddress()
{
var httpRequest = HttpContext.Current.Request;
var id = httpRequest["personId"];
int personId;
if (!Int32.TryParse(id, out personId)) return null;
using (var db = new ApplicationDbContext())
{
var preAddress = (from pa in db.PersonAndAddresses
join a in db.Addresses on pa.AddressId equals a.AddressId
join b in db.PersonBarangays on a.BarangayCode equals b.BarangayCode
join m in db.PersonMunicipals on a.MunicipalCode equals m.MunicipalCode
join p in db.PersonProvinces on a.ProvinceCode equals p.ProvinceCode
join r in db.PersonRegions on a.RegionCode equals r.RegionCode
where pa.PersonId == personId
select new PreAddress()
{
BarangayCode = b.BarangayName,
AddressId = a.AddressId,
HouseNumber = a.HouseNumber,
MunicipalCode = m.MunicipalName,
ProvinceCode = p.ProvinceName,
RegionCode = r.Region,
StreetName = a.StreetName,
UnitNumber = a.UnitNumber,
VillageSubdivision = a.VillageSubdivision
});
return preAddress;
}
}
This is how I get the data from the client:
service
getAddress() {
const endpoint = this.rootUrl + '/api/getaddress';
const formData: FormData = new FormData();
formData.append('personId', this.genParams.personId);
return this.http.post(endpoint, formData);
}
component
getPersonInformation() {
this.clientService.getPerson(this.genParams.personId)
.subscribe((data: any) => {
console.log(data);
this.person = data;
});
}
Following the server using debugger, I can actually get a value but in my client side. I get the following error:
I need your help. Thank you.
Try updating your code like this:
[HttpPost]
[Route("api/getaddress")]
public PreAddress GetAddress()
{
var httpRequest = HttpContext.Current.Request;
var id = httpRequest["personId"];
int personId;
if (!Int32.TryParse(id, out personId)) return null;
PreAddress preAddress;
using (var db = new ApplicationDbContext())
{
var preAddress = (from pa in db.PersonAndAddresses
join a in db.Addresses on pa.AddressId equals a.AddressId
join b in db.PersonBarangays on a.BarangayCode equals b.BarangayCode
join m in db.PersonMunicipals on a.MunicipalCode equals m.MunicipalCode
join p in db.PersonProvinces on a.ProvinceCode equals p.ProvinceCode
join r in db.PersonRegions on a.RegionCode equals r.RegionCode
where pa.PersonId == personId
select new PreAddress()
{
BarangayCode = b.BarangayName,
AddressId = a.AddressId,
HouseNumber = a.HouseNumber,
MunicipalCode = m.MunicipalName,
ProvinceCode = p.ProvinceName,
RegionCode = r.Region,
StreetName = a.StreetName,
UnitNumber = a.UnitNumber,
VillageSubdivision = a.VillageSubdivision
});
preAddress = preAddress.FirstOrDefault();
}
return preAddress;
}