簡體   English   中英

Function 正在返回花括號

[英]Function is returning curly braces

我以前問過非常類似的問題( 為什么將 object 發布到 json 文件時,其中一個鍵/值總是顯示為花括號? ),並且討厭再次問它但我無法在任何地方找到答案。 所以我有這個 function 返回“你好”(它應該返回一個特定的值,但是錯誤發生在任何一種方式,所以我有“你好”用於測試)。

function index(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            $("#noteDisplay").on("click", ".remove", (e) => {
                //indexResult = (e.currentTarget.parentNode.id - 1);
                const indexResult = "hello";
                let error = false;
                //-----
                if(!error){
                    resolve(indexResult);
                } else {
                    reject('Something went wrong!');
                }
            });
        }, 2000);
    });
}      

我在這里稱之為 function。

export default function SendId(){
    React.useEffect(() => {
        /*$("#noteDisplay").on("click", ".remove", async (e) => {
            console.log("ola");
        });*/
        //-----------------------------------------------------
        $("#noteDisplay").on("click", ".remove", async (e) => {
            const PORT = process.env.PORT || 3001;
        
            let index_note = { // the object
                index: await index()
            }
        
            let data = JSON.stringify(index_note);
        
            let xhr = new XMLHttpRequest();
            xhr.open('POST', `http://localhost:${PORT}/api/1`, true);
            xhr.send(data);
            console.log("the data was sent")
        });
        
    });
    return(null);
}

無論有沒有異步等待,當我記錄它時,它總是會重新調整大括號。 我以這種方式進行控制台記錄(但我認為這不是問題所在)。

app.post("/api/:1", (req, res) =>{
  console.log(req.body);
});

對於重復的問題,我們深表歉意,如有任何幫助,我們將不勝感激。

您沒有將 'Content-Type': 'application/json' 添加到您的請求標頭中。 也許這是個問題

xhr.setRequestHeader('Content-Type', 'application/json');

暫無
暫無

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

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