I’m trying to pass an array with ids to a backend in node.js. When console.log(Products) run before the axios.post(), the Products array has two ids in it. When I console.log(req.body.products) in my backend, the array is empty. How can I still access the array in my backend?
Frontend code
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)
})
Backend code
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
})
})