【问题标题】:error in creating charge coinbase commerce through post request通过发布请求创建收费 coinbase 商务时出错
【发布时间】:2021-09-24 17:19:02
【问题描述】:

我正在尝试在 coinbase 商务中创建费用

const query = {
    name: 'The Sovereign Individual',
    description: 'Mastering the Transition to the Information Age',
    local_price: {
        amount: '100.00',
        currency: 'USD',
    },
    pricing_type: 'fixed_price',
    metadata: {
        customer_id: 'id_1005',
        customer_name: 'Satoshi Nakamoto',
    },
    redirect_url: 'https://charge/completed/page',
    cancel_url: 'https://charge/canceled/page',
};
const opts = {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-CC-Api-Key': '6225fe4d-f9a8-498f-8085-11dea4212316',
        'X-CC-Version': '2018-03-22',
    },
    body: JSON.stringify({ query }),
};
const res = fetch('https://api.commerce.coinbase.com/charges', opts).then(
    (res) => res.json()
);

这是我当前的发帖请求,但它显示错误,我认为是格式问题,但有人可以帮助我吗?

【问题讨论】:

    标签: javascript http-post coinbase-api


    【解决方案1】:

    您需要在寻求帮助之前提供您收到的错误消息,无论哪种方式,在查看您的代码后我发现了一些问题。

    在这部分

    const res = fetch('https://api.commerce.coinbase.com/charges', opts).then(
        (res) => res.json()
    );
    

    您不能将fetch 的结果分配给一个常量,因为您使用.then 来处理promise,并且返回值被传递给回调,第二个res.json() 也返回一个promise,并且您'不处理它。

    这将解决上述问题

    const res = await fetch('https://api.commerce.coinbase.com/charges', opts);
    const jsonRes = await res.json();
    // using async await
    

    如果你不想使用异步/等待

    fetch('https://api.commerce.coinbase.com/charges', opts)
    .then(res => res.json())
    .then(jsonRes => console.log(jsonRes));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-03
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多