簡體   English   中英

為什么數組不能通過 axios.post 傳遞?

[英]Why won't an array pass with axios.post?

我正在嘗試將帶有 id 的數組傳遞給 node.js 中的后端。 當 console.log(Products) 在 axios.post() 之前運行時,Products 數組中有兩個 id。 當我在后端 console.log(req.body.products) 時,數組為空。 我怎樣才能在我的后端訪問數組?

前端代碼

const Products = []

                        tempProd.forEach((item) => {
                            firebase.firestore().collection('products').where('name', '==', item.name).get().then((querySnapshot) => {
                                querySnapshot.forEach((doc) => {
                                    Products.push({
                                        price: doc.data().priceID,
                                    })
                                })
                            })
                        })

                        console.log(Products)

                            axios.post('https://Kvam-E-sport-API.olsendaniel04.repl.co/invoice', {
                                products: Products,
                                cus_id: response.data.medlem.id,
                            }).then((response) => {
                                console.log(response)
                            })

后端代碼

app.post('/invoice', cors(), async (req, res) => {
      console.log(req.body)
      subscription = await stripe.subscriptions.create({
        collection_method: 'send_invoice',
        customer: req.body.cus_id,
        items: req.body.products,
        days_until_due: 14,
      })

    res.json({
      subscription: subscription
    })
  })

正在使用正文解析器? 例如。

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM