2017-08-12 96 views
1

我遇到的問題是我的反應原生應用與我的Node API交互。 當我嘗試發佈數據以通過節點更新數據庫中的某些值(然後轉到存儲過程)時,我得到一個500 - 在節點中發送後無法設置標題。反應本機/節點API:發送後無法設置標題

我能找到的所有東西都說這可能是由於發送了兩次響應。我不認爲這是這種情況。我已經在郵遞員測試和事情工作正常,它返回正確的返回數據200的狀態。

我試圖將數據發佈到API,像這樣:

const myHeaders = new Headers(); 
    myHeaders.append('Content-Type', 'application/json'); 


    fetch(`http://localhost:3000/user/preferences`, { 
     method: 'POST', 
     headers: myHeaders, 
     mode: 'cors', 
     cache: 'default', 
     body: JSON.stringify({ 
     id: '1', 
     minage: this.state.minageValue, 
     maxage: this.state.maxageValue 
     }) 
    }) 
     .then(response => response.json()) 
     .then(body => console.log(body)) 
     .catch(err => console.log(err)); 

我收到它的API側,將數據傳遞到存儲過程與此:

function updatePreferences(req, res, next) { 
    console.log(req); 
    var userid = req.body[0].id; 
    var minage = req.body[0].minage; 
    var maxage = req.body[0].maxage; 

    db.func('___spsavepreferences', [userid, minage, maxage]) 
     .then(function(data){ 
      res.status(200) 
       .json({ 
        status: 'success', 
        preferences: data 
       }); 
     }); 
} 

任何想法?

編輯:

不知道該告訴我什麼,但我已記錄的錯誤消息到控制檯:

Error: Can't set headers after they are sent. 
at validateHeader (_http_outgoing.js:504:11) 
at ServerResponse.setHeader (_http_outgoing.js:511:3) 
at ServerResponse.header (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:730:10) 
POST /user/preferences 500 410.809 ms - 189 
    at ServerResponse.send (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:170:12) 
    at done (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:967:10) 
    at Object.exports.renderFile (/Users/user/Projects/Project_node/node_modules/jade/lib/index.js:374:12) 
    at View.exports.__express [as engine] (/Users/user/Projects/Project_node/node_modules/jade/lib/index.js:417:11) 
    at View.render (/Users/user/Projects/Project_node/node_modules/express/lib/view.js:128:8) 
    at tryRender (/Users/user/Projects/Project_node/node_modules/express/lib/application.js:640:10) 
    at Function.render (/Users/user/Projects/Project_node/node_modules/express/lib/application.js:592:3) 
    at ServerResponse.render (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:971:7) 
    at /Users/user/Projects/Project_node/app.js:51:7 
    at Layer.handle_error (/Users/user/Projects/Project_node/node_modules/express/lib/router/layer.js:71:5) 
    at trim_prefix (/Users/user/Projects/Project_node/node_modules/express/lib/router/index.js:315:13) 
    at /Users/user/Projects/Project_node/node_modules/express/lib/router/index.js:284:7 
    at Function.process_params (/Users/user/Projects/Project_node/node_modules/express/lib/router/index.js:335:12) 

編輯2:知道了。我需要接受值爲req.body.id,req.body.minage等...而不是req.body [0] .id。 奇怪的是得到這樣的錯誤,但解決了錯誤。

回答

0

如果你看到在郵遞員不同的行爲,這是因爲郵差只是做了POST而直接在瀏覽器嘗試POST之前,而不是做一個CORS preflight OPTIONS request

所以,它聽起來就像是造成該你看到的是,OPTIONS要求,而不是POST請求「後,他們被送到不能設置頭」的要求。

您可以確認通過向郵遞員發送OPTIONS請求,並確保還提供Origin請求標頭作爲請求的一部分。隨着curl可以是這樣做的:

curl -X OPTIONS -i -H 'Origin: http://x.y' http://localhost:3000/user/preferences 

這可能會產生相同的500錯誤你從瀏覽器請求觀看。

至於如何解決它,我認爲沒有辦法可以告訴你沒有看到更多的服務器端代碼。但看起來似乎是,在你的代碼的某個地方,你有一個或多個邏輯處理OPTIONS請求的地方,並且在那個OPTIONS處理代碼的某個地方,它試圖在發送頭之後發送頭。

爲了追蹤到底是哪裏,你似乎希望查看服務器端的服務器日誌,並查找發生500次故障時記錄的任何消息。

順便說一句,在這種情況下,觸發瀏覽器做COR預檢OPTIONS請求開始的東西是myHeaders.append('Content-Type', 'application/json')部分。

+0

嗨sideshow,謝謝你的迴應。我可能是完全錯誤的,但這似乎不是問題。我通過curl發送了選項請求,並得到了200的響應 - 這似乎表明所有的都在那裏......我也有多個其他API端點(看起來幾乎完全相同,只是不同的存儲過程名稱和參數)工作正常。我用錯誤消息更新了我的帖子。該應用程序的其餘部分幾乎是一個標準的節點安裝 - 我沒有任何改變。 – nab1994

+0

行不知道多少答案實際上幫助,但我確實看到你從你的更新到你設法追查原因並修復它的問題,所以我很高興聽到你找到解決方案:) – sideshowbarker

相關問題