POST request for nested objects - Angular

Discussão em 'Angular' iniciado por Beth McCoy, Fevereiro 7, 2025.

  1. Beth McCoy

    Beth McCoy Guest

    I'm currently trying to allow users to post hotels using an angular front end. This is supported with a python backend. Certain attributes such as 'lat', 'lng' and 'html_attributions' are nested within objects. You can see this layout within the python backend:

    new_hotel = {
    "name": request.form["name"],
    "address": request.form["address"],
    "rating":request.form["rating"],
    "geometry": {
    "location": {
    "lat": request.form["lat"],
    "lng": request.form["lng"]
    }
    },
    "photos": [
    {
    "html_attributions": [
    request.form["html_attributions"]
    ]
    }
    ]


    I'm unsure how to allow users to POST a new hotel because of this. Currently, my web service looks like:

    postHotel(hotel:any){
    let postData = new FormData();
    postData.append('name', hotel.name);
    postData.append('address', hotel.address);
    postData.append('rating', hotel.rating);
    postData.append('lat', hotel.geometry.location.lat );
    postData.append('lng', hotel.geometry.location.lng );
    postData.append('photo', hotel.photos.html_attributions);


    return this.http.post(
    'http://localhost:5000/api/v1.0/hotels', postData
    );

    }


    I'm however getting the error 'ERROR TypeError: Cannot read properties of undefined (reading 'location')'

    Any help with this would be greatly appreciated!

    Continue reading...

Compartilhe esta Página