[英]NextJS calling a API after request
我在 BigCommerce 上使用网络钩子。 当用户将商品添加到购物车时,他们会调用我的 api/webhooks/addtocart。
我在那里写了以下代码。
import {NextApiRequest, NextApiResponse} from 'next';
let axios = require("axios").default;
export default async function product(req: NextApiRequest, res: NextApiResponse) {
try {
let id = req.body.data.id;
console.log(id)
res.send('OK')
} catch (error) {
const {message, response} = error;
res.status(response?.status || 500).json({message});
}
}
使用 id 我可以获得购物车 ID。 我想使用该购物车 ID 获取该购物车上的数据。 所以我需要创建一个 GET 到https://api.bigcommerce.com/stores/{store_hash}/v3/carts/{cartId}
我在res.send('OK')
行之前写了这段代码。 我从来没有返回结果。
var options = {
method: 'GET',
url: 'https://api.bigcommerce.com/stores/{store_hash}/v3/carts/{id}',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Auth-Token': 'aqqeqzgjinaow54rzzalyk6wban6wa4'
}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
有人帮我完成这些吗?
非常感谢您的帮助,因为我是新来的。
谢谢你的时间
axios.request(options)
返回一个promise ,这意味着您需要await
它,或者在.then
处理程序中使用res.send
。
使用await
的简化版本:
export default async function product(req: NextApiRequest, res: NextApiResponse) {
let id = req.body.data.id;
const { data } = await axios.request({ method: 'GET', ... });
console.log(data)
res.send('OK')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.