无法res.send()服务器响应,但能够使用Express控制台.log相同的响应

时间:2020-06-30 17:17:45

标签: node.js express routes

我正在使用Express路由开发自定义集成解决方案,并且对Firebase进行访存调用后,我需要对服务器的响应是来自Firebase的响应,才能向我显示来自以下方面的任何问题(例如身份验证错误)在那里。

我一直试图通过使用res.send()显示响应,但是它抛出“ TypeError:将圆形结构转换为JSON”错误,但是当console.logging相同的响应时,它给了我正确的响应(这是身份验证错误)。怎么了?

这是代码:

router.route("/bankval").get (fetchAirtabeleRecords, (req, res) => {

    fetch(`https://xxxxxxxxxxxx.firebaseio.com/integratedData.json?auth=${FIREBASESECRET}`,{
    method: 'PUT',
    headers:
    {
        Authorization: 'Bearer ' + FIREBASESECRET,
        'Content-Type': 'application/json'
    },

    body: JSON.stringify({bankValuation: res.bankVal}) // correct values here
})

.then((res) => res.json())

.then(res.send(res)) // This throws me the error

.then((res)=> { console.log('response: ', res); }) // This works and displays expected "error: 'Unauthorized request.' from firebase, but it's only in console, so it's not good enough."


// .then(res.send({bankValuation: res.bankVal}))   // this works, but if authentication error occurs it still displays the correct data, when it's obviously not sent to firebase. Only using this for testing purposes.

.catch(err=> { console.log('error: ',err); }) })

我对此很陌生,所以也许我正在完全后退或进行其他操作,但是任何输入都值得赞赏。

谢谢。

1 个答案:

答案 0 :(得分:1)

您覆盖res,请尝试以下操作:

.then((resp) => resp.json())

.then((resp) => res.send(resp))

.then((resp)=> { console.log('response: ', resp); })