我的代码是这样的:
SiteModel.find(
{},
function(docs) {
next(null, { data: docs });
}
);
但它永远不会返回任何内容...但如果我在 {} 中指定某些内容,那么就会有一条记录。那么,如何找到全部?
尝试使用此代码进行调试:
SiteModel.find({}, function(err, docs) {
if (!err) {
console.log(docs);
process.exit();
}
else {
throw err;
}
});
2017 年 Node 8.5 方式
try {
const results = await SiteModel.find({});
console.log(results);
} catch (err) {
throw err;
}
来自文档:
let result = SiteModel.find({}, function (err, docs) {});
或者使用 async wait 你也可以这样做:
let result = await SiteModel.find({});
const result = await SiteModel.find()
- 没有 {}
功能中的 .find()
也可以工作。
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: "未找到记录", }) ); };