1
votes

My Mongoose Function

Error Message: Cast to ObjectId failed for value "count" at path "_id" for model "myinfo""

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,
		});
	});
};
1

1 Answers

3
votes

The problem was with my route

  1. Since i already have a route "/myinfo/:id". my mistake was i am using "/myinfo/count" route for getting count values which is wrong. because count is mapped as :id in my above route
  2. i changed the "/myinfo/count" to "/myinfo/get/count" it works now.