Axios TypeError: Converting circular structure to JSON

Marty

I have the following functions in a Node application:

Middleware

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();
  ...
}

Service function:

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;
};

The line const posts: any = await axios.get(https://url.com); in the service function doesn't seem to work. What am I doing wrong here?

Dean Van Greunen

load the post's data or status variable, as posts is actually a axios response object

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;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sequelize: TypeError: Converting circular structure to JSON

GraphCool TypeError: Converting circular structure to JSON

"TypeError: Converting circular structure to JSON" with GET request

"TypeError: Converting circular structure to JSON" with GET request

Elliptic : TypeError : Converting circular structure to JSON

Angular - TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in nodejs

avoiding TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON - find error in json

TypeError converting circular structure to json at JSON.stringify (<anonymous>)

Reading fixture is throwing error TypeError: Converting circular structure to JSON

how to solve (Uncaught TypeError: Converting circular structure to JSON)

Moving code to an exported function results in TypeError: Converting circular structure to JSON

[Vue warn]: Error in render: "TypeError: Converting circular structure to JSON

Express Middleware: ERROR: TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in retrieving html string from mongodb

Firestore references create a "TypeError: Converting circular structure to JSON"

Chrome sendrequest error: TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON property 'name' closes the circle

TypeError: Converting circular structure to JSON when trying to POST request

TypeError: Converting circular structure to JSON with StorybookJS on React Native

Doing a proper MongoDB Query (TypeError: Converting circular structure to JSON)

VM1374:32 Uncaught TypeError: Converting circular structure to JSON(…)

ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FormGroup'

Amazon Cognito confirmPassword fails with (TypeError: Converting circular structure to JSON)

TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'

Uncaught TypeError: Converting circular structure to JSON after Angular / dependencies update

How do I solve "TypeError: Converting circular structure to JSON" in NodeJS

TypeError: Converting circular structure to JSON -- Next.js