我有一個React向后端發出axios請求的前端。
在后端,在某些情況下,我需要發出另一個 axios 請求。
但是 theaxios.patch不運行:它似乎執行了該行,但之后它不執行 the.then也不執行該.error行。
我在router.patch('/stripeUser', (req, res, next) => {}函式中設定了一個斷點,但它沒有被觸發。
如果我嘗試呼叫相同的呼叫 use Postman,它會成功。
這是后端代碼:
router.post('/', async (req, res, next) => {
const params = {
user: req.body.data[0].user,
}
const customer = params.user.stripeId !== undefined
? await stripe.customers.retrieve(params.user.stripeId)
: await stripe.customers.create({
email: params.user.email
});
if (params.user.stripeId === undefined && customer.id !== undefined) {
axios.patch('https://xxx.domain.com/users/update', { data: [{ userId: params.user.userId, stripeId: customer.id }] })
.then((usr) => {
console.log("User updated: stripeId: " usr.user.stripeId)
newCustomer = usr.user;
})
.catch(error => {
console.log(error);
});
}
const intermediate = usr.user.someTransform();
res(intermediate) ???
return intermediate ???
第二個問題:
如果axios.fetch應該運行,我如何將其結果傳遞usr給程式的其余部分?
uj5u.com熱心網友回復:
您是否嘗試過使用await而不是then,因為該函式可能會在 axios 決議請求回應之前回傳,如果您不等待決議,您可能無法在同一函式中使用您在 promise 回呼中獲得的資料。
const usr = await axios.patch('https://xxx.domain.com/users/update',
{ data: [{ userId: params.user.userId, stripeId: customer.id }] })
.catch(error => console.log(error));
const newCustomer = usr.user;
// ...
uj5u.com熱心網友回復:
axios 補丁呼叫中缺少結束引號。
axios.patch('https://xxx.domain.com/users/update', { data: [{ userId: params.user.userId, stripeId: customer.id }] })
.then((usr) => {
console.log("User updated: stripeId: " usr.user.stripeId)
newCustomer = usr.user;
})
.catch(error => {
console.log(error);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443521.html
標籤:节点.js axios nodejs-服务器
