2016-01-29 12 views
3

使用stencil-utils,我可以檢索給定產品ID的產品信息。但是,我找不到任何關於如何將信息作爲JSON響應檢索的文檔,而不是HTML模板。使用Stencil Utils檢索產品信息爲JSON

現在,我使用下面的代碼:

utils.api.product.getById(
    847, 
    {}, 
    (err, resp) => { 
     console.log(resp); 
    } 
) 

我希望有一個參數,我可以在params對象傳遞將返回響應,JSON,所以我可以只提取信息我需要關於產品。

回答

3

使用{ params: { debug: "context" } }將努力利用stencil start創造了當地的環境很好,但是一旦你捆綁&上傳你的主題直播網站,它停止工作。生產時禁用調試工具debug: "context"debug: "bar"

伸手的Bigcommerce支持,原來掛我這太問題後,似乎這是他們提出的工作流程:

你將不得不使用一個虛擬的車把模板,包括你所需要的變量,並使用由bigcommerce提供的自定義手柄幫手,{{json}} - 似乎剛剛運行JSON.stringify() - 幫手被定義爲here

utils.api.product.getById(
    847, { template: 'path/to/template' }, (err, resp) => { 
     // Will print the name of the product. 
     console.log(resp.product.title); 
    }); 

我已經成功與path/to/templatecustom/template-name和放置車把模板在templates/components/custom文件夾中。我還沒有測試過從另一個來源傳遞模板。

1

因此,看起來您可以通過將param對象添加到選項來傳遞其他參數。使用debug: "context",您可以檢索頁面的整個上下文,然後通過訪問response.product來獲取產品信息。

utils.api.product.getById(
    847, 
    { params: { debug: "context" } }, 
    (err, resp) => { 
     // Will print the name of the product. 
     console.log(resp.product.title); 
    } 
)