我正在學習后端 Web 開發,我正在學習的課程包括為具有“游覽”的系統制作 REST API。我使用 MongoDB 和 Mongoose,一切都很好,我根據引數過濾查詢,一切順利。但是后來講師想把這個過濾程序(和其他查詢程序)放到一個類中,并在這個類中訪問它們。完成此操作后,我開始收到此錯誤。所以我知道您不能將圓形物件轉換為 JSON,也不能將這樣的物件字串化。但我沒有任何...圓形物體。或者至少我不這么認為。我只是一個初學者,所以如果你愿意,請原諒我的錯誤。這是代碼:
const Tour = require("../models/tourModel"); //MongoDB Document Model
class QueryModifier {
constructor(query, reqQuery) {
this.query = query;
this.reqQuery = reqQuery;
}
filter() {
console.log(this.reqQuery);
const queryObj = { ...this.reqQuery }; //destruct the URL query object into a new object for a hard-copy
const toBeExcluded = ["page", "sort", "limit", "fields"];
toBeExcluded.forEach((el) => {
delete queryObj[el];
});
console.log(queryObj);
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(
/\b(gte|gt|lte|lt)\b/g,
(matchedStr) => `$${matchedStr}`
);
this.query = this.query.find(JSON.parse(queryStr));
return this;
}
sort() {
if (this.reqQuery.sort) {
const sortBy = this.reqQuery.sort.replace(/(,)/g, " ");
this.query = this.query.sort(sortBy);
} else {
this.query = this.query.sort("-createdAt");
}
return this;
}
limitFields() {
if (this.reqQuery.fields) {
const fields = this.reqQuery.fields.replace(/(,)/g, " ");
this.query = this.query.select(fields);
} else {
//Default
this.query = this.query.select("-__v"); //exclude __v
}
return this;
}
paginate() {
const page = this.reqQuery.page * 1 || 1;
const limit = this.reqQuery.limit * 1 || 10;
const skip = (page - 1) * limit;
this.query = this.query.skip(skip).limit(limit);
return this;
}
}
exports.getAllTours = async (req, res) => {
try {
const features = new QueryModifier(Tour.find(), req.query)
.filter()
.sort()
.limitFields()
.paginate();
const tours = await features;
res.status(200).json({ //<------ Error's "position" is after the second dot.
status: "success",
results: tours.length,
data: { tours },
});
} catch (e) {
res.status(404).json({
status: "failure",
message: e.message,
});
console.log(e);
}
};
錯誤是在我用箭頭評論的地方。這是錯誤訊息:
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Topology'
| property 's' -> object with constructor 'Object'
| property 'sessionPool' -> object with constructor 'ServerSessionPool'
--- property 'topology' closes the circle
at JSON.stringify (<anonymous>)
at stringify (/Users/mirzabicer/Documents/GitHub/complete-node-bootcamp/4-natours/starter/node_modules/express/lib/response.js:1123:12)
at ServerResponse.json (/Users/mirzabicer/Documents/GitHub/complete-node-bootcamp/4-natours/starter/node_modules/express/lib/response.js:260:14)
at exports.getAllTours (/Users/mirzabicer/Documents/GitHub/complete-node-bootcamp/4-natours/starter/controllers/tourController.js:84:25)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
我將該 JSON.stringify 呼叫編輯為:
let queryStr = '{ "sort": "price,ratingsAverage", "price": { "gte": "300" } }';
所以現在我的代碼中實際上沒有 JSON.stringify 呼叫。但我得到的錯誤完全相同。
uj5u.com熱心網友回復:
該錯誤表明問題出在tourController.js第 84 行。我猜您正在呼叫JSON.stringify該行上的圓形內容。嘗試洗掉該JSON.stringify呼叫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/394853.html
標籤:javascript 节点.js MongoDB 表达 过滤
