POST请求用于嵌套对象 – Angular

huangapple go评论61阅读模式
英文:

POST request for nested objects - Angular

问题

我目前正在尝试允许用户使用Angular前端发布酒店。这是通过Python后端支持的。某些属性,如'lat'、'lng'和'html_attributions',嵌套在对象中。您可以在Python后端中看到这个布局:

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"]
            ]
        }
    ]
}

由于这个原因,我不确定如何允许用户发布新酒店。目前,我的Web服务如下:

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

然而,我收到了错误消息:“ERROR TypeError: Cannot read properties of undefined (reading 'location')”。

对此有任何帮助将不胜感激!

英文:

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!

答案1

得分: 1

我建议采用强类型表单的方法。首先,你需要为你的酒店数据创建一个接口:

interface IHotel {
  name: string;
  address: string;
  rating: string;
  geometry: {
    location: {
      lat: number;
      lng: number;
    }
  };
  photos: Array<{
    html_attributions: Array<{
      html_attributions: string; // 猜测
    }>;
  }>;
}

接下来,你需要创建一个强类型表单来接受用户输入:

import { FormArray, FormControl, FormGroup } from '@angular/forms';

export interface IHotelForm {
  name: FormControl<string>,
  address: FormControl<string>,
  rating: FormControl<string>,
  geometry: FormGroup<{
    location: FormGroup<{
      lat: FormControl<number>,
      lng: FormControl<number>,
    }>,
  }>,
  photos: FormArray<FormGroup<{
    html_attributions: FormControl<string>,
  }>>,
}

现在,当你将数据传递给你的服务时,你可以确保结构和类型是正确的。

class YourService {

  postHotel(hotel: IHotel): Observable<IHotel> {
    return this.httpClient.post<IHotel>(yourUrl, hotel);
  }

}

是的,描述这些结构需要一些时间,但好处绝对超出了时间投资。

当然,如果你的照片数据包含文件,你需要创建一个多部分表单数据对象。

英文:

I would recommend taking the strongly typed form approach. First, you'll need an interface for your hotel data:

interface IHotel {
  name: string;
  address: string;
  rating: string;
  geometry: {
    location: {
      lat: number;
      lng: number;
    }
  };
  photos: Array&lt;{
    html_attributions: Array&lt;{
      html_attributions: string; // guessing here
    }&gt;;
  }&gt;;
}

Next, you'll need a strongly-typed form to accept user input:

import { FormArray, FormControl, FormGroup } from &#39;@angular/forms&#39;;

export interface IHotelForm {
  name: FormControl&lt;string&gt;,
  address: FormControl&lt;string&gt;,
  rating: FormControl&lt;string&gt;,
  geometry: FormGroup&lt;{
    location: FormGroup&lt;{
      lat: FormControl&lt;number&gt;,
      lng: FormControl&lt;number&gt;,
    }&gt;,
  }&gt;,
  photos: FormArray&lt;FormGroup&lt;{
    html_attributions: FormControl&lt;string&gt;,
  }&gt;&gt;,
}

Now when you hand off the data to your service, you can be assured that the structure and types are correct.

class YourService {

  postHotel(hotel: IHotel): Observable&lt;IHotel&gt; {
    return this.httpClient.post&lt;IHotel&gt;(yourUrl, hotel);
  }

}

Yes, it takes some time to describe these structures, but the benefits far outweigh the investment in time.

Of course, if your photos data consists of files, you'll need to create a multi-part form data object.

huangapple
  • 本文由 发表于 2023年3月7日 05:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定