[英]TypeError: Converting circular structure to JSON in mongoDB/mongoose
我的代码中出现以下错误。 另一件事是第 3 行的常量“Product”未定义,我不知道为什么。 请帮帮我。 整个代码在这个链接的github上
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
代码 -
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,
});
}
};
在发送响应之前,您需要等待查询返回数据。
您可以在查询前使用await
const products = await Product.find();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.