首页 > 解决方案 > NODEJS - 即使成功完成,从另一个端点调用一个端点也会陷入 CATCH

问题描述

我有这两个端点(它被简化了,但想法/问题保持不变)......

/api/v1/更新

exports.update = (req, res) => {

  Data.update({salary: 1})
  .then(data => {
    console.log("All good")
    return res.status(200).send({message: "OK"})
  })
  .catch(error => {
    console.log("Oh Crap!")
    return res.status(400).send(error)
  })

}

/api/v1/process我在其中调用第一个作为处理的一部分

exports.process = (req, res) => {

  axios.get('http://localhost:8008/api/v1/update')
  .then(data => {
    console.log("You got it")
    return res.status(200).send(data)
  })
  .catch(error => {
    console.log("Not working!")
    return res.status(400).send(error)
  })

}

这就是我在控制台中得到的:

$>: nodemon server.js
Server running on port 8008
All good
Not working!

为什么 axios 调用成功(“更新”端点)完成但它落入 CATCH 而不是 THEN ?

我错过了什么?

编辑:

好的,我想通了..错误在return res.status(200).send(data)/api/v1/process ...

错误:将循环结构转换为 JSON

所以我所做的是我提取了值(第一个端点的响应->更新)并在之后发送它:)它现在工作得很好......愚蠢的错误

掌心

标签: node.js

解决方案


错误出现在 /api/v1/process 的返回 res.status(200).send(data) ...

错误:将循环结构转换为 JSON

所以我所做的是我提取了值(第一个端点的响应->更新)并在之后发送它:)它现在工作得很好......愚蠢的错误


推荐阅读