Asp.Net Core Webapi Returning Object List

Asked

Viewed 480 times

0

I am trying to return a list of a "Plan" Template that has a list of "Expenses", returning only the plan list works, but when added to return the spending list of a javascript error.

"Failed to load Resource: net::ERR_SPDY_PROTOCOL_ERROR"

"Uncaught (in Promise) Typeerror: Failed to fetch"

Controller

    [HttpPost]
    public IEnumerable<Plano> GetPlanos()
    {
        return db.GetPlanos()
            .AsEnumerable();
    } 

Modelo dos Gastos

[DataContract]
public class Gastos
{
    [DataMember]
    public long Id { get; set; }
    [DataMember]
    public string Descricao { get; set; }
    [DataMember]
    public TipoFrequencia Frequencia { get; set; }
    [DataMember]
    public TipoGasto Tipo { get; set; }
    [DataMember]
    public DateTime? Data { get; set; }
    [DataMember]
    public double Valor { get; set; }
    [DataMember]
    public Plano Planos { get; set; }
}

Flat Model

[DataContract]
public class Plano
{
    [DataMember]
    public long Id { get; set; }      
    [DataMember]
    public List<Gasto> Gasto { get; set; }
    [DataMember]
    public int Moradores { get; set; }

}

Javascript call

componentDidMount() {
    fetch('Plano/GetPlanos', { method: 'POST' })
        .then(response => response.json())
        .then(data => {
            console.log(data);
            this.setState({ lista: data, loading: false });
        });   
}

Query in the database

    public IQueryable<Plano> GetPlanos()
    {
        return _contexto.Planos
                    .Include(p => p.Gastos);                        
    }

Not even this console.log runs, the error happens before, if I take [Datamember] out of the spending list works.

Debugging on Asp.net da to see that it’s all right he picks up the data all right, only at the time to return to the javascript that the problem.

  • Put that part in your question: db.GetPlanos() what this method does?

  • ready, I made an adjustment in the controller that was wrong tbm, but the error remains the same

  • 1

    because you’re using the verb post to get a get? o. The

  • anyway, the way you describe the problem, it seems to me that it is in the API and not in React...

1 answer

2


Leo, this seems like a mistake circular reference which generally creates an exception at the time of serialization. Add the line below in the method ConfigureServices class Startup.cs, this tends to solve this problem:

services.AddMvc()
    .AddJsonOptions(
        options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    );
  • reference: https://docs.microsoft.com/en-us/ef/core/querying/related-data#Related-data-and-serialization

  • 1

    It was circular reference msm, I was adding the Plan List to the expense list, I just removed the [Datamember] from the Plan List and it was, vlw

Browser other questions tagged

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