我的 Mongoose 函数
错误消息:模型“myinfo”的路径“_id”处的值“count”转换为 ObjectId 失败
exports.indexCount = function(req, res) {
MyInfo.countDocuments({}, function(err, count) {
if (err) {
res.json({
status: "error",
message: err,
});
}
console.log("Number of users:", count);
res.json({
status: "success",
message: "Count info retrieved successfully",
data: count,
});
});
};
最佳答案
问题出在我的路线
- 因为我已经有一条路线“/myinfo/:id”。我的错误是我正在使用 用于获取计数值的“/myinfo/count”路线是错误的。 因为 count 在我上面的路由中被映射为 :id
- 我将“/myinfo/count”更改为“/myinfo/get/count”,现在可以使用了。
关于node.js - 在 Mongoose 中调用 modal.countDocuments() 和 modal.count() 时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759375/