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,
});
}
};
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,
});
}
};
Share
Improve this question
asked Oct 21, 2020 at 15:52
Akash ShyamAkash Shyam
1141 silver badge10 bronze badges
0
1 Answer
Reset to default 8You 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();