stack questions
stack questions

Reputation: 982

Could not get the data from IQueryable<object>

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: enter image description here

I need your help. Thank you.

Upvotes: 0

Views: 40

Answers (2)

Marcus H&#246;glund
Marcus H&#246;glund

Reputation: 16811

You need to execute the IQueryable before you return it, in this scenario try with ToList()

[HttpPost]
[Route("api/getaddress")]
public IEnumerable<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.ToList(); //invoke it here
    }
}

Upvotes: 1

Mohsin Mehmood
Mohsin Mehmood

Reputation: 4236

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; 
    }

Upvotes: 2

Related Questions