2017-10-09 55 views
1

我正在使用'官方'Shopify節點適配器(https://github.com/MONEI/Shopify-api-node)並嘗試創建訂單草稿。使用shopify npm模塊創建訂單的錯誤請求

經由郵差發送的

{ 
    "draft_order": { 
    "line_items": [ 
     { 
     "title": "Custom Tee", 
     "price": "20.00", 
     "quantity": 2 
     } 
    ] 
    } 
} 

作品有效載荷,但返回從API '壞請求'(400)。

全功能/通過包裝調用如下:

import Shopify from 'shopify-api-node'; 

makeDraftOrder: function(shop_name) { 
    console.log('trying to connect with ', shop_name); 
    const shop = Shops.findOne({'shopName': shop_name}); 

    const shopify_data = new Shopify({ 
     shopName: shop.shopName, 
     accessToken: shop.accessToken 
    }); 

    let newOrder = JSON.stringify({ 
     "draft_order": { 
     "line_items": [ 
      { 
      "title": "Custom Tee", 
      "price": "20.00", 
      "quantity": 2 
      } 
     ] 
     } 
    }); 

    shopify_data.draftOrder.create(newOrder).then(data => { 
     console.log('draft order', data); 
    }).catch(err => console.error('wawawoowa', err)); 
    } 

製作到draftOrder.list()呼叫工作正常,但上面的失敗。任何幫助非常感謝。

回答

0

不要用"draft_order": { ... }包裝訂單。您也不需要將對象串聯起來。

let newOrder= { 
    "line_items": [ 
    { 
     "title": "Custom Tee", 
     "price": "20.00", 
     "quantity": 2 
    } 
    ] 
}; 

shopify_data.draftOrder.create(newOrder) 
+0

不錯,謝謝。你能告訴我你是如何知道如何消除這兩種情況的? – Rockafella

+0

我前段時間有同樣的問題。我從'shopify-api-node'的源代碼中找出了它。在這裏,您可以看到一行內容爲''draft_order「:{...}'的內部包裝:https://github.com/MONEI/Shopify-api-node/blob/2.9.0/index.js# L116。 'key'在這裏設置:https://github.com/MONEI/Shopify-api-node/blob/2.9.0/resources/draft-order.js#L18 –

+0

從'JSON.stringify'開始,shopify -api-node'使用'got'模塊發出請求。它有一個[json](https://www.npmjs.com/package/got#json)模式選項,在這裏設置爲true:https://github.com/MONEI/Shopify-api-node/blob/ 2.9.0/index.js#L101,所以你必須傳遞一個普通的對象,並將其引入JSON。 –