35

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?

5 Answers 5

82

Try this code to debug:

SiteModel.find({}, function(err, docs) {
    if (!err) { 
        console.log(docs);
        process.exit();
    }
    else {
        throw err;
    }
});
2
  • Any ideas why this wouldn't work when I run this from the terminal like node filename.js?
    – M. Ali
    Commented Dec 14, 2015 at 8:50
  • Note you don't even need the empty curly braces if you are using a then-catch like paradigm.
    – Stan Luo
    Commented May 23, 2017 at 0:29
25

The 2017 Node 8.5 way

try {
  const results = await SiteModel.find({});
  console.log(results);
} catch (err) {
  throw err;
}
5

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({});
2

const result = await SiteModel.find() - Without the {} in the .find() function works as well.

0

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

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 20 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.