多次调用Axios时update函数未触发的技术求助
排查update函数未触发的问题
核心问题:/send-money接口未返回响应
你的/send-money接口执行完PayPal请求后,没有向调用方(axios)返回任何响应。axios的then回调需要等到请求收到完整响应才会执行,导致当前接口一直处于pending状态,后续的更新交易请求根本没机会触发。
修复方法:在/send-money的try和catch块里添加响应返回:
app.post("/send-money", async (req, res) => { const request = new paypal2.payouts.PayoutsPostRequest(); const { receiverEmail, amount} = req.body; request.requestBody({ sender_batch_header: { sender_batch_id: `batch-${Math.random()}`, email_subject: 'You have a payment', email_message: 'You have received a payment! Thanks for using our service.' }, items: [{ recipient_type: 'EMAIL', amount: { value: amount, currency: 'USD' }, receiver: receiverEmail, note: 'Thank you for your service!', sender_item_id: `item-${Math.random()}` }] }); try { console.log("hallo"); const response = await paypalClient.execute(request); console.log(`Payout created with ID: ${response.result.batch_header.payout_batch_id}`); // 添加成功响应 res.status(200).send({ message: 'Payout created successfully', payoutBatchId: response.result.batch_header.payout_batch_id }); } catch (err) { console.error(`Error creating payout: ${err.message}`); // 添加错误响应 res.status(500).send({ error: 'Failed to create payout', details: err.message }); } })
其他优化与排查点
- 等待所有异步操作完成
当前代码创建了payoutPromises但未用Promise.all等待,会导致接口提前返回res.json(transactions),后续打款和更新操作仍在后台执行,可能出现请求结束但操作未完成的情况。修改如下:
if (transactionDetails.length > 0) { const payoutPromises = transactionDetails.map(details => axios.post('http://localhost:3000/send-money', { amount: details.amount, receiverEmail: details.receiverEmail }) .then(async response => { console.log('Payout posted successfully:', response.data); try { const updateResponse = await axios.post('http://localhost:3000/edit-transaction', { id: details.id, go: false }); console.log('Transaction updated successfully:', updateResponse.data); } catch (updateError) { console.error('Error updating transaction:', updateError); } }) .catch(error => { console.error('Error paying out transaction or updating transaction:', error); }) ); // 等待所有异步操作完成 await Promise.all(payoutPromises); }
- 添加请求日志排查
在/edit-transaction接口里打印请求参数,确认是否有请求进入:
app.post('/edit-transaction', async (req, res) => { console.log('Received update request:', req.body); // 新增日志 const { id, go} = req.body; try { console.log("as") await program.updatePaypalTransaction(id, go); res.status(200).send({ message: 'Transaction updated successfully' }); } catch (err) { console.error('Error updating transaction:', err); res.status(500).send('Internal Server Error'); } });
排查步骤
- 优先修复
/send-money的响应问题,这是更新未触发的最可能原因。 - 启动服务后调用
/solana-transactions,查看控制台日志:- 若看到
Payout posted successfully,说明打款请求已完成,继续查看是否有Received update request日志。 - 若未看到
Payout posted successfully,说明打款请求处于pending状态,确认为响应未返回的问题。
- 若看到
内容的提问来源于stack exchange,提问作者Nguyễn Nam Vinh
相关产品推荐
相关产品推荐

