使用Stencil Utils以JSON格式检索产品信息 [英] Retrieve product information as JSON with Stencil Utils

查看:106
本文介绍了使用Stencil Utils以JSON格式检索产品信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 stencil-utils ,我可以检索给定产品ID的产品信息。但是,我找不到任何关于如何将信息作为JSON响应检索而不是HTML模板的文档。

Using stencil-utils, I can retrieve product information for a given product ID. However, I can't find any documentation on how to retrieve the information as a JSON response as opposed to an HTML template.

现在,我正在使用以下内容代码:

Right now, I'm using the following code:

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

我希望我可以在params对象中传递一个参数,它将以JSON的形式返回响应,这样我就可以只提取产品所需的信息。

I'm hoping there's a parameter I can pass in the params object that will return the response as JSON so I can extract just the information I need about the product.

推荐答案

使用 {params:{debug:context}} 将在本地工作得很好使用模板开始创建的环境,但是一旦你捆绑&将您的主题上传到实际网站,它将停止工作。调试工具 debug:context debug:bar在生产时被禁用。

Using { params: { debug: "context" } } will work great on the local environment created using stencil start, however once you bundle & upload your theme to a live site, it stops working. The debugging tools debug: "context" and debug: "bar" are disabled on production.

在接触到bigcommerce支持后,最初将我链接到这个SO问题,这似乎是他们提议的工作流程:

After reaching out to bigcommerce support, which originally linked me to this SO question, it seems this is their proposed workflow:

你必须使用虚拟把手模板,包含你需要的变量,并使用bigcommerce提供的自定义把手助手, {{json}} - 这似乎只是运行 JSON.stringify() - 帮助程序已定义这里

You will have to use a dummy handlebars template, include the variables you need, and use the custom handlebars helper provided by bigcommerce, {{json}} - which seems to just run JSON.stringify() - the helper is defined here.

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

我已成功使用路径/到/模板 custom / template-name 并将把手模板放在 templates / components / custom 文件夹中。我没有测试过从其他来源传递模板。

I have had success with path/to/template being custom/template-name and placing the handlebars template in the templates/components/custom folder. I have not tested passing it a template from another source.

这篇关于使用Stencil Utils以JSON格式检索产品信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆