簡體   English   中英

在 axios.post - firebase 中發送多個對象

[英]Sending multiple objects in axios.post - firebase

我正在嘗試使用 axios.post 發送多個對象,如下所示:

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

但它似乎只有在其中一個是空的時才有效,而不是同時兩個。 如果它們都有一些值,則不會發送調度。 順便說一句,fullPrice 是直接值,而不是 object。 為什么會這樣,我應該如何正確發送帶有多個對象的 axios.posts?

嘗試在一個數組中發送多個對象。

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

使其成為這樣的對象列表,我假設您的 api 將接受 object 的列表,但不接受 object 的對象列表。

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

如果這不起作用,請查看后端並查看他們接受的數據類型。 我很確定您應該發送一組對象,如果沒有,請查看后端。

終於修好了。 那是最愚蠢的事情,我曾經遇到過,並且從沒想過這是問題所在......
只需添加 event.preventDefault(); 給你的處理程序。 真的,像魅力一樣工作

 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);
    }

在我的操作文件中,我操作連接到數據庫的所有內容

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));
            });
    }
}

暫無
暫無

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

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