Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Angular HTTP client-如何在默认情况下发送带有所有attribute`null的POST HTTP对象`_Angular_Angular Httpclient - Fatal编程技术网

使用Angular HTTP client-如何在默认情况下发送带有所有attribute`null的POST HTTP对象`

使用Angular HTTP client-如何在默认情况下发送带有所有attribute`null的POST HTTP对象`,angular,angular-httpclient,Angular,Angular Httpclient,我的角度分量: const p: Product = this.products.find((d) => d === event.item.data); p.name = 'foo'; 我的角度服务是: updateProduct(product: Product): Observable<CommonResult> { return this.http.put<CommonResult>(this.configService.getApiUri() +

我的角度分量:

const p: Product = this.products.find((d) => d === event.item.data);
p.name = 'foo';
我的角度服务是:

updateProduct(product: Product): Observable<CommonResult> {
    return this.http.put<CommonResult>(this.configService.getApiUri() + '/products/' + product.productId, product);
}
我想:

{
  id: 1
  name: "foo",
  category: null
}
但我有:

{
  id: 1
  name: "foo"
}

我无法访问我的后端代码(我无法更改后端代码)。如何修补我的前端代码以修复我的问题?

您永远不会在类外创建对象,因此永远不会分配
category=null
。您将该类用作接口,声明属性,但从未创建其实例

export class Product {
    id: number;
    name: string;
    category: string = null;
}
为了将类别设置为null,您必须使用
新产品()
,并可能为其他属性设置构造函数:

const productResult = this.products.find((d) => d === event.item.data);
const p: Product = new Product(productResult.id, 'foo');
具有构造函数的产品类:

export class Product {
    id: number;
    name: string;
    category: string = null;

    constructor(id: number, name: string) {
      this.id = id;
      this.name = name;
    }
}

现在,您的对象将
类别设置为null

我修补我的更新,因为我的GET不返回类别:null

{...new Product(), ...product}
完整代码:

updateProduct(product: Product): Observable<CommonResult> {
    return this.http.put<CommonResult>(this.configService.getApiUri() + '/products/' + product.productId, {...new Product(), ...product});
}
updateProduct(产品:产品):可观察{
返回this.http.put(this.configService.getApiUri()+'/products/'+product.productId,{…新产品(),…产品});
}

在将产品实例发送到后端之前,向我们展示创建和填充该产品实例的代码。我用这些信息编辑了我的问题,找到了一个现有产品,并更改了其名称。正在创建的现有产品在哪里?无论如何,问题是类别设置为未定义,或者产品实际上不是类的实例,或者类别被删除。在发送类别之前,将其显式设置为null。
updateProduct(product: Product): Observable<CommonResult> {
    return this.http.put<CommonResult>(this.configService.getApiUri() + '/products/' + product.productId, {...new Product(), ...product});
}