Webhook 响应数据因 'map' 未定义而失败
Webhook reponse data failing with 'map' of undefined
我正在尝试在更新购物车时从 Shopify Webhook 获取数据。它一直有效,直到我尝试映射我得到的响应。
显示错误:类型错误:无法读取未定义的 属性 'map'。
但是如果我 console.log() 数据,它会按应有的方式显示数组。但是还是不能map()它。
async afterAuth(ctx) {
...
const { shop, accessToken } = ctx.session;
ctx.cookies.set('shopOrigin', shop, {
httpOnly: false,
secure: true,
sameSite: 'none'
});
await getSubscriptionUrl(ctx, accessToken, shop);
const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });
router.post('/webhooks/carts/update', webhook, (ctx) => {
// This console log works
console.log(ctx.state.webhook);
const cartProducts = ctx.state.webhook.payload.line_items.product_id;
// This console log is not working = undefined
console.log('cartProducts: >>> ',cartProducts);
var productListIds = cartProducts.map(item => {
var product = item.product_id.toString();
return product
})
console.log('ARRAY OF PRODUCT IDS: ',productListIds);
});
...
我希望“cartProducts”可用于 map()。 async/await 会有帮助吗?如果是这样,我该如何实施?
提前致谢
将 cartProducts 变量创建为 ctx.state.webhook.payload.line_items 因为只有个别项目有 product_id 属性。
在使用 map() 函数之前,您已尝试从数组访问 product_id 属性。
async afterAuth(ctx) {
...
const {
shop,
accessToken
} = ctx.session;
ctx.cookies.set('shopOrigin', shop, {
httpOnly: false,
secure: true,
sameSite: 'none'
});
await getSubscriptionUrl(ctx, accessToken, shop);
const webhook = receiveWebhook({
secret: SHOPIFY_API_SECRET_KEY
});
router.post('/webhooks/carts/update', webhook, (ctx) => {
// This console log works
console.log(ctx.state.webhook);
// Removed the 'product_id'
const cartProducts = ctx.state.webhook.payload.line_items;
console.log('cartProducts: >>> ', cartProducts);
var productListIds = cartProducts.map(item => {
var product = item.product_id.toString();
return product
})
console.log('ARRAY OF PRODUCT IDS: ', productListIds);
});
...
我正在尝试在更新购物车时从 Shopify Webhook 获取数据。它一直有效,直到我尝试映射我得到的响应。
显示错误:类型错误:无法读取未定义的 属性 'map'。
但是如果我 console.log() 数据,它会按应有的方式显示数组。但是还是不能map()它。
async afterAuth(ctx) {
...
const { shop, accessToken } = ctx.session;
ctx.cookies.set('shopOrigin', shop, {
httpOnly: false,
secure: true,
sameSite: 'none'
});
await getSubscriptionUrl(ctx, accessToken, shop);
const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });
router.post('/webhooks/carts/update', webhook, (ctx) => {
// This console log works
console.log(ctx.state.webhook);
const cartProducts = ctx.state.webhook.payload.line_items.product_id;
// This console log is not working = undefined
console.log('cartProducts: >>> ',cartProducts);
var productListIds = cartProducts.map(item => {
var product = item.product_id.toString();
return product
})
console.log('ARRAY OF PRODUCT IDS: ',productListIds);
});
...
我希望“cartProducts”可用于 map()。 async/await 会有帮助吗?如果是这样,我该如何实施?
提前致谢
将 cartProducts 变量创建为 ctx.state.webhook.payload.line_items 因为只有个别项目有 product_id 属性。 在使用 map() 函数之前,您已尝试从数组访问 product_id 属性。
async afterAuth(ctx) {
...
const {
shop,
accessToken
} = ctx.session;
ctx.cookies.set('shopOrigin', shop, {
httpOnly: false,
secure: true,
sameSite: 'none'
});
await getSubscriptionUrl(ctx, accessToken, shop);
const webhook = receiveWebhook({
secret: SHOPIFY_API_SECRET_KEY
});
router.post('/webhooks/carts/update', webhook, (ctx) => {
// This console log works
console.log(ctx.state.webhook);
// Removed the 'product_id'
const cartProducts = ctx.state.webhook.payload.line_items;
console.log('cartProducts: >>> ', cartProducts);
var productListIds = cartProducts.map(item => {
var product = item.product_id.toString();
return product
})
console.log('ARRAY OF PRODUCT IDS: ', productListIds);
});
...