NodeJS Axios类型错误:将循环结构转换为JSON

wmomyfyw  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(129)

我在Node应用程序中有以下函数:
中间件

const recachePosts: any = async (req: Request, res: Response, next: NextFunction) => {
    try {
      const status = await cachePosts();
      ...
    } catch (err: any) {
      return res.status(422).json({
        status: false,
        message: err.message,
      });
    }
}

const cachePosts = async () => {
  const posts: any = await fetchAllPosts();
  ...
}

字符串
服务功能:

const fetchAllPosts = async () => {
  console.log('Step 1');
  const posts: any = await axios.get(`https://url.com`);
  console.log('Step 2: ' + posts);
  // returns Step 2: [Object Object]
  console.log('Step 2: ' + JSON.stringify(posts));
  // returns TypeError: Converting circular structure to JSON
  return posts;
};


service函数中的const posts: any = await axios.get( https://url.com );行似乎不起作用。我做错什么了?

ddrv8njm

ddrv8njm1#

加载post的数据或状态变量,因为posts实际上是axios响应对象

const fetchAllPosts = async () => {
  console.log('Step 1');
  const posts = await axios.get(`https://url.com`);
  
  // Log specific properties of the 'posts' object
  console.log('Step 2 - Data: ', posts.data);
  console.log('Step 2 - Status: ', posts.status);

  return posts;
};

字符串

相关问题