简体   繁体   中英

Sending multiple objects in axios.post - firebase

I'm trying to send multiple objects with axios.post like that:

  export const onOrderSent = (deliveryData,orderData,fullPrice) => {
    return dispatch => {
        let data = {deliveryData,orderData,fullPrice};
        dispatch(sendOrderStart());
        axios.post('/orders.json', data)
            .then(response => {
                ...
            })
            .catch(error => {
                ...
            });
    }
}

But it seems to work only if one of them is empty, never two at the same time. If they both have some values, the dispatch is not being sent. btw, fullPrice is a straight value, not an object. Why is that, and how should I properly send axios.posts with multiple objects?

Try sending multiple objects in an Array.

const data = {objects: [deliveryData, orderData, fullPrice]};

Make it a list of objects like this, i would assume your api would accept a list of object but not a object of objects.

        let data = {[deliveryData,orderData,fullPrice]};

If this doesn't work look into the backend and see what type of data they're accepting. i'm pretty sure you should be sending an array of objects, if not, look in the backend.

Finally fixed it. That was the stupidest thing, i have ever came across ever, and would never expect it was the problem...
Just add event.preventDefault(); to your handler. Really, working like a charm

 orderSendHandler = (event) => {
        event.preventDefault();
        ...
        ///im mapping local state to object here, and then
        const data = {
            deliveryData: formData,
            order: this.props.orderedItems,
            price: this.props.fullPrice
        };
        this.props.onOrderSent(data);
    }

and in my actions file, where i operate everything connected to database

export const onOrderSent = (data) => {
    return dispatch => {
        dispatch(sendOrderStart());
        console.log(data);
        axios.post('/orders.json', data)
            .then(response => {
                console.log(response);
                dispatch(sendOrderSuccess());
            })
            .catch(error => {
                console.log(error);
                dispatch(sendOrderFail(error));
            });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM