My code is like that:
SiteModel.find(
{},
function(docs) {
next(null, { data: docs });
}
);
but it never returns anything... but if I specify something in the {} then there is one record. so, how to findall?
My code is like that:
SiteModel.find(
{},
function(docs) {
next(null, { data: docs });
}
);
but it never returns anything... but if I specify something in the {} then there is one record. so, how to findall?
Try this code to debug:
SiteModel.find({}, function(err, docs) {
if (!err) {
console.log(docs);
process.exit();
}
else {
throw err;
}
});
then-catch
like paradigm. –
Charmine The 2017 Node 8.5 way
try {
const results = await SiteModel.find({});
console.log(results);
} catch (err) {
throw err;
}
From the documentation:
let result = SiteModel.find({}, function (err, docs) {});
or using async await you can do like this also:
let result = await SiteModel.find({});
const result = await SiteModel.find()
- Without the {}
in the .find()
function works as well.
exports.getAllUsers = (req, res) => { userSchema .find() .then((data) => res.status(200).json({ status: "success", results: data.length, data: { data, }, }) ) .catch((err) => res.status(404).json({ status: "error", message: "Not records found", }) ); };
© 2022 - 2024 — McMap. All rights reserved.
node filename.js
? – Extraterritorial