WDI-SEA / project-4-issues

Open an issue to receive help on project 4 issues
0 stars 0 forks source link

Problem creating a user pet #38

Closed lawrencesalinas closed 2 years ago

lawrencesalinas commented 2 years ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

What's the problem you're trying to solve?

We are having a problem creating a user pet

Post any code you think might be relevant (one fenced block per file)

## POST route
  def post(self, request):
        pet_user = request.user
        pet_data = Pet(pet_owner = pet_user)
        pet = PetSerializer(pet_data, data=request.data)
        if pet.is_valid():
            # Save the created mango & send a response
            pet.save()
            return Response(pet.data, status=status.HTTP_201_CREATED)
        # If the data is not valid, return a response with the errors
        return Response(pet.errors, status=status.HTTP_400_BAD_REQUEST)

----------------------------------------------------
POST request
var axios = require('axios');
    var data = JSON.stringify({
      "pet": {
        name: e.target.name.value,
        pet_owner: props.user
      }
    });

    var config = {
      method: 'POST',
      url: `http://localhost:8000/pets`,
      headers: { 
        'Authorization': `Token ${props.user.token}`,
        'Content-Type': 'application/json', 
      },
      data : data
    };
    axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

xhr.js:210 POST http://localhost:8000/pets 400 (Bad Request) createError.js:16 Uncaught (in promise) Error: Request failed with status code 400 at createError (createError.js:16:1) at settle (settle.js:17:1) at XMLHttpRequest.onloadend (xhr.js:66:1)

What is your best guess as to the source of the problem?

it's either the post route or the react post request

What things have you already tried to solve the problem?

I tried changing the request.user on post route to pet_owner = request.user.id, it still gave me errors of 500

lawrencesalinas commented 2 years ago

Updated the serializers code for related pet name correlating to user