I want to count the number of documents from the database. But getting Note.count is not a function error.
Here’s my code
<code>const Note = require("../models/Notes.js");
const mongoose = require("mongoose");
exports.dashboard = async (req, res) => {
let perPage = 12;
let page = req.query.page || 1;
const locals = {
title: "Dashboard",
description: "Free NodeJS Notes App.",
};
try {
const notes = await Note.aggregate([
{
$sort: { updatedAt: -1 },
},
{
$match: { user: new mongoose.Types.ObjectId(req.user.id) },
},
{
$project: {
title: { $substr: ["$title", 0, 30] },
body: { $substr: ["$body", 0, 100] },
},
},
])
.skip(perPage * page - perPage)
.limit(perPage)
.exec();
const count = Note.count();
res.render("dashboard/index", {
userName: req.user.firstName,
locals,
notes,
layout: "../views/layouts/dashboard",
current: page,
pages: Math.ceil(count / perPage),
});
} catch (error) {
console.log(error);
}
};
</code>
<code>const Note = require("../models/Notes.js");
const mongoose = require("mongoose");
exports.dashboard = async (req, res) => {
let perPage = 12;
let page = req.query.page || 1;
const locals = {
title: "Dashboard",
description: "Free NodeJS Notes App.",
};
try {
const notes = await Note.aggregate([
{
$sort: { updatedAt: -1 },
},
{
$match: { user: new mongoose.Types.ObjectId(req.user.id) },
},
{
$project: {
title: { $substr: ["$title", 0, 30] },
body: { $substr: ["$body", 0, 100] },
},
},
])
.skip(perPage * page - perPage)
.limit(perPage)
.exec();
const count = Note.count();
res.render("dashboard/index", {
userName: req.user.firstName,
locals,
notes,
layout: "../views/layouts/dashboard",
current: page,
pages: Math.ceil(count / perPage),
});
} catch (error) {
console.log(error);
}
};
</code>
const Note = require("../models/Notes.js");
const mongoose = require("mongoose");
exports.dashboard = async (req, res) => {
let perPage = 12;
let page = req.query.page || 1;
const locals = {
title: "Dashboard",
description: "Free NodeJS Notes App.",
};
try {
const notes = await Note.aggregate([
{
$sort: { updatedAt: -1 },
},
{
$match: { user: new mongoose.Types.ObjectId(req.user.id) },
},
{
$project: {
title: { $substr: ["$title", 0, 30] },
body: { $substr: ["$body", 0, 100] },
},
},
])
.skip(perPage * page - perPage)
.limit(perPage)
.exec();
const count = Note.count();
res.render("dashboard/index", {
userName: req.user.firstName,
locals,
notes,
layout: "../views/layouts/dashboard",
current: page,
pages: Math.ceil(count / perPage),
});
} catch (error) {
console.log(error);
}
};
I am not able to get the error in my code. Can anyone help me with the error.