1

I am getting the following error in my code. Another thing is that the constant 'Product' on line 3 is undefined and I don't know why. Please help me out. The entire code is on github at this link

TypeError: Converting circular structure to JSON
--> starting at object with constructor 'NativeTopology'
|     property 's' -> object with constructor 'Object'
|     property 'sessionPool' -> object with constructor 'ServerSessionPool'
--- property 'topology' closes the circle
at JSON.stringify (<anonymous>)
at stringify (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/response.js:1123:12)
at ServerResponse.json (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/response.js:260:14)
at module.exports.getAllProducts (/Users/akash/Desktop/Projects/cheaplops/controllers/productController.js:14:19)
at Layer.handle [as handle_request] (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/layer.js:95:5)
at /Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:335:12)
at next (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:174:3)
at router (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:317:13)
at /Users/akash/Desktop/Projects/cheaplops/node_modules/express/lib/router/index.js:284:7

Code -

const mongoose = require('mongoose');

const Product = require('../models/productsModel');
const catchAsync = require('../utils/catchAsync');

module.exports.getAllProducts = async (req, res, next) => {
    try {
        // Get all products
        const products = Product.find();

        // console.log(products);

        // Send response
        res.status(200).json({
            status: 'success',
            results: products.length,
            data: {
                products,
            },
        });
    } catch (error) {
        console.log(error);
        res.status(404).json({
            status: 'fail',
            err: error,
        });
    }
};
0

1 Answer 1

8

You need to wait for the query to return data before sending the response.

You can use await in front of the query

const products = await Product.find();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @Vishnu
I had the same problem but await resulted in a syntax error because my function was not labeled "async". So yes, 'await' but also make sure your function is async.
Yep, this was the problem. *facepalm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.