Node.js 无法res.send()服务器响应,但可以使用Express console.log记录相同的响应

Node.js 无法res.send()服务器响应,但可以使用Express console.log记录相同的响应,node.js,express,routes,Node.js,Express,Routes,我正在使用Express routes开发一个自定义集成解决方案,在调用firebase后,我需要firebase对服务器的响应,以向我显示由此产生的任何问题(如身份验证错误) 我一直试图使用res.send()显示响应,但它抛出了一个“TypeError:将循环结构转换为JSON”错误,但是当console.logging记录相同的响应时,它会给我正确的响应(这是一个身份验证错误)。怎么回事 代码如下: router.route("/bankval").get (fetch

我正在使用Express routes开发一个自定义集成解决方案,在调用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); }) })
我是个新手,所以也许我完全是在倒退,但任何意见都是值得赞赏的


谢谢。

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

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

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

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

哦,老兄!成功了!!!非常感谢你。我花了好几个小时想弄明白。你能简单地详细介绍一下交易内容吗?再次感谢。您在这里有
res
变量
(req,res)=>{
,然后当您在下面写
res
时,在这里
(res)=>res.json()
您覆盖了自定义
res
变量,该变量具有方法
send
。因此,当您执行
res.send
时,您的
res
已经只是JSON,而没有方法
send