Hasura Action调用GraphQL报错parsing HashMap failed问题咨询
问题原因
你调用execute发起GraphQL请求时的variables参数传值不符合Hasura要求:
GraphQL接口要求variables必须是键值对对象,键名要和你查询语句里定义的变量名完全匹配。你现在直接把orders数组([247, 260, 253])传给了variables参数,所以Hasura解析时预期拿到对象,实际收到数组就抛出了对应报错。
修复方案
修改调用execute的那行代码,把数组包装成符合要求的变量对象:
// 原错误写法 // const { data:driverPaymentData, errors:driverPaymentError} = await execute(ACTION_INSERT_PAYSLIP_GET_DRIVER_PAYMENT_DATA, orders) // 修正后写法,也可以简写为 {orders} const { data:driverPaymentData, errors:driverPaymentError} = await execute(ACTION_INSERT_PAYSLIP_GET_DRIVER_PAYMENT_DATA, {orders: orders})
提取目标字段的示例代码
拿到正确返回后,可以用遍历的方式提取stage_cost_driver_id和delivery_salary:
const result = [] // 遍历所有返回的订单 driverPaymentData.company_order.forEach(order => { // 遍历订单下的司机明细 order.company_order_details.forEach(detail => { result.push({ driverId: detail.stage_cost_driver_id, salary: detail.company_user.delivery_salary }) }) }) // 如果需要全局对司机去重,可以用Map处理: const uniqueDrivers = Array.from(new Map( result.map(item => [item.driverId, item]) ).values())
内容的提问来源于stack exchange,提问作者ganjaam
相关产品推荐
相关产品推荐

