【问题标题】:Can not access a key value in node object无法访问节点对象中的键值
【发布时间】:2015-08-13 19:54:42
【问题描述】:

我什至无法弄清楚这是怎么可能的

当我这样做时:

console.error(order.Items[i]);

我明白了:

{ ItemURL: '',
  IsBundle: false,
  GiftTaxPrice: '0',
  GiftPrice: '0',
  GiftNotes: null,
  GiftMessage: null,
  RecyclingFee: 0,
  ShippingTaxPrice: 0,
  ShippingPrice: 6,
  TaxPrice: 0,
  UnitPrice: 3,
  Quantity: 1,
  Title: 'HICKIES TEST PRODUCT',
  Sku: 'PLSB_TEST_PRODUCT',
  SiteListingID: '30000418',
  SiteOrderItemID: '',
  ProductID: 72176,
  OrderID: 100152,
  ProfileID: 12021605,
  ID: 156,
  CustomFields: [],
  Adjustments: [],
  FulfillmentItems: [],
  Promotions: [] }

但由于某种原因,当我尝试使用

访问 Sku
console.error(order.Items[i].Sku);

我明白了:

undefined

由于某种原因神奇地

console.error(order.Items[i].Quantity);

console.error(order.Items[i].UnitPrice);

打印:

1

3

分别

---编辑

根据要求

 for (var i = 0; i < order.Items.length; i++) {
    formatedOrder['Subtotal'] += parseInt(order.Items[i].Quantity) * parseFloat(order.Items[i].UnitPrice);

    console.error(order.Items[i]);
    console.error(order.Items[i].Sku);
    console.error(order.Items[i]['Sku']);


    formatedOrder["OrderLines"].push({
        "Product": order.Items[i].Sku,
        "Quantity": parseInt(order.Items[i].Quantity),
        "Price": parseFloat(order.Items[i].UnitPrice)
    });
}

根据请求的项目数组:

[{ ItemURL: '',
  IsBundle: false,
  GiftTaxPrice: '0',
  GiftPrice: '0',
  GiftNotes: null,
  GiftMessage: null,
  RecyclingFee: 0,
  ShippingTaxPrice: 0,
  ShippingPrice: 6,
  TaxPrice: 0,
  UnitPrice: 3,
  Quantity: 1,
  Title: 'HICKIES TEST PRODUCT',
  Sku: 'PLSB_TEST_PRODUCT',
  SiteListingID: '30000418',
  SiteOrderItemID: '',
  ProductID: 72176,
  OrderID: 100152,
  ProfileID: 12021605,
  ID: 156,
  CustomFields: [],
  Adjustments: [],
  FulfillmentItems: [],
  Promotions: [] }]

【问题讨论】:

  • 什么是“i”,它是如何被赋值的?如果您共享更多代码,您将获得更好的结果。
  • 出于兴趣,你会从 console.error(order.items[i]['Sku']) 得到什么?
  • 代码位于随机函数中,用于重新格式化对象以提交给专有 api。老实说,我认为我正在失去理智,这段代码怎么会不起作用。以及 console.error(order.Items[i]['Sku']); 会发生什么的问题。输出仍未定义
  • 您的代码有效,这是一个演示:jsfiddle.net/hvzpdan2
  • 同意ochi。看到这个jsfiddle.net/0vpe6cxv

标签: javascript node.js express


【解决方案1】:

试试这个,我没理由用console.error() 改用console.log()

for (var i = 0; i < order.Items.length; i++) {
    formatedOrder['Subtotal'] += parseInt(order.Items[i].Quantity) * parseFloat(order.Items[i].UnitPrice);

    console.log(order.Items[i]);
    console.log(order.Items[i].Sku);
    console.log(order.Items[i]['Sku']);


    formatedOrder["OrderLines"].push({
        "Product": order.Items[i].Sku,
        "Quantity": parseInt(order.Items[i].Quantity),
        "Price": parseFloat(order.Items[i].UnitPrice)
    });
}

嗯,好惊讶!!!

【讨论】:

  • 我刚刚使用了控制台错误,因此它会在我的控制台中突出显示,与正在打印的输出相对应。事实证明,我并没有像我预期的那样传递一个扁平的物体。对对象进行字符串化就像一种魅力。
  • 试试这个答案如果这不起作用我会删除它。这个对我有用。我不知道 JS 可以提供堆栈跟踪。你的 log.error 在 FF 中给了我一个堆栈跟踪
【解决方案2】:

JSON.Stringify() 解决了这个问题,事实证明该对象不是我预期的扁平 JSON 对象,而是其他一些不允许我访问值的对象

【讨论】:

  • 这就是为什么我要求看看Items是什么样子的(数组,不只是一个元素)......基本上是为了确认所有元素的结构(不仅仅是你的那个已发布)
猜你喜欢
  • 2019-04-03
  • 2015-01-06
  • 2018-03-27
  • 2019-10-07
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多