Error inserting documents into Mongodb

Asked

Viewed 84 times

0

I made these Inserts by the shell of the Mongo:

db.Operators.insert([{ 
    Name:'Paulo de Tarso',
    Version:1
}, { 
    Name:'José de Arimatéia',
    Version:1
}, { 
    Name:'Catarina Silva', 
    Version:3
}])

This is my Entity in the API

public class Operator : Entity, IAggregateRoot
{
    public string Name { get; protected set; }
    public int Version { get; protected set; }

    public Operator(string name)
    {
        this.Name = name;
    }
}

I have the following scenarios:

By giving a Get for the Postman, I catch this mistake:

Internal Server Error

This mistake happens because my class of request come empty

My controller

[AllowAnonymous]
[HttpGet(Name ="GetAllOperators")]
public async Task<IActionResult> GetOperators()
{
    List<OperatorResult> operatorResults = await operatorsQueries.GetOperator();
    List<OperatorDetailsModel> operatorDetailsModels = new List<OperatorDetailsModel>();
    operatorResults.ForEach(x => operatorDetailsModels.Add(new OperatorDetailsModel(x.OperatorId, x.Name)));

    var defaultReturn = new DefaultReturn(operatorResults, false, string.Empty);

    return new ObjectResult(defaultReturn);
}

My Getoperator of the operatorsQueries class

public async Task<List<OperatorResult>> GetOperator()
{
    List<Operator> operators = await context.Operators.Find(x => true).ToListAsync();
    List<OperatorResult> operatorResults = resultConverter.Map<List<OperatorResult>>(operators);
    return operatorResults;
}

The lambda picks up here

public void OnException(ExceptionContext context)
{
    DomainException domainException = context.Exception as DomainException;
    if (domainException != null)
    {
        string json = JsonConvert.SerializeObject(domainException.BusinessMessage);
        var defaultReturn = new DefaultReturn(new { }, true, domainException.BusinessMessage);
        context.Result = new BadRequestObjectResult(defaultReturn);
        context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
    }
}

Here is the mistake, because mine domainException is coming NULL and should not.

In the other scenario is when I insert Postman document. In this case, I don’t have this error, but the Version field comes (0) because it is an int.

I just didn’t test with GUI tools for Mongo, like Robo 3T, for example.

  • I believe that this problem happens when I insert the document in the "hand". My Api should not recognize, because if I input through Postman, this error does not happen.

  • This null because it has context.Exception as DomainException. Who assures you that the exceptions generated will always be of the type DomainException? You need to see the original exception and your exception treatment is generating another exception...

  • Another thing, but not related to the problem, what is the purpose of using protected set?

  • I can’t explain it to you, @LINQ, I’d have to talk to the API guys, but they don’t stay on my floor, at least not this API. But the problem has been solved as they removed the Version field, it will no longer be necessary and I close the post

  • Close the question then, if possible. Because it’s not making any sense and there’s no way to help someone in the future, then...

No answers

Browser other questions tagged

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