我必须向服务器和响应对象发送请求
export class OrderResponse{
constructor(public OrderStatus: string, public Details: string, public Changes: string, public OrderNum?: number){}
}
我尝试在函数中使用它:
this.orderService.createOrder(this.orderService.order).subscribe(
res => {
this.orderResponse = res;
//this.router.navigate(["orders", res.OrderNum]);
console.log(this.orderResponse);
console.log(this.orderResponse.OrderNum);
},
error => console.log(error)
);
当我记录完整的响应对象时,它会记录成功:
{"OrderNum":62828,"OrderStatus":"Rejected","Details":null,"Changes":null}
但是当我尝试记录OrderNum属性时,它会记录未定义。怎么可能,该对象具有属性值,但它是否未定义?
答案 0 :(得分:0)
当我询问console.log(Object.keys(this.orderResponse))
显示你说的话时,你的澄清说明了:
[“0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,..
这意味着orderResponse
对象实际上是一个数组(我认为是订单)
如果这些是订单,则console.log(this.orderResponse[0].OrderNum)
应该为您提供所需内容(对于第一个结果,其他结果将0更改为列表的所需订单索引)。
答案 1 :(得分:0)
避免未定义的错误。使用
skipWhile
运营商 if(condition)
this.orderService
.createOrder(this.orderService.order)
.skipWhile(data=>return data.length)///////////////
.subscribe(res => {
if(res){///////////////
this.orderResponse = res;
//this.router.navigate(["orders", res.OrderNum]);
console.log(this.orderResponse);
console.log(this.orderResponse.OrderNum);
}
},(error => console.log(error));