Node.js mongoose find()返回模型的属性
我正在尝试使用mongoose查找现有用户 我的问题是Node.js mongoose find()返回模型的属性,node.js,mongoose,Node.js,Mongoose,我正在尝试使用mongoose查找现有用户 我的问题是 UserAccount.find(variables, function (error, success) { if (error) { response.send({status: false}); } else { console.log(success); } }); 若用户存在,
UserAccount.find(variables, function (error, success) {
if (error) {
response.send({status: false});
} else {
console.log(success);
}
});
若用户存在,则表示它返回以下数组
[ model {
'$__':
InternalCache {
strictMode: true,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
saving: undefined,
version: undefined,
getters: {},
_id: 5c98e64f2106e94022532c9f,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: [StateMachine],
pathsToScopes: {},
session: null,
ownerDocument: undefined,
fullPath: undefined,
emitter: [EventEmitter],
'$options': [Object] },
isNew: false,
errors: undefined,
_doc:
{ isActive: true,
_id: 5c98e64f2106e94022532c9f,
userName: 'buyer@mysite.com',
password:
'$2a$05$vpowA76cB3T/4eHGbQPqd.F/iIebX7SXKPZA2k1wcmlSIDks0q852',
userCategory: 'buyer',
createdDate: 2019-03-20T14:31:43.250Z,
updatedDate: 2019-03-20T14:31:43.250Z,
__v: 0 },
'$init': true } ]
我不知道是什么导致了这个问题?直到昨天它才返回用户数据,但这对我来说太奇怪了。如何解决这个问题?有人能帮我修一下吗?谢谢。您最初获得的额外属性是由于结果是模型实例的集合,这些实例附带了普通对象上不可用的额外属性和方法。 您可以将
{lean:true}
作为选项传递给get plain对象,而不需要所有这些附加属性和方法
UserAccount.find(variables, {lean: true}, function (error, success) {
if (error) {
response.send({status: false});
} else {
console.log(success);
}
});
我也面临同样的问题,问题是如果您将最新版本的mongo与较旧的mongoose一起使用,这可以通过安装较新的mongoose版本并运行mongoose find()来解决,这将解决您的问题。您只需在查询结束时使用lean()即可解决此问题
const courses = await Course
.find({ isPublished: true })
.or([
{ price: { $gte: 15 } },
{ name: /.*by.*/i }
]).lean(); // use lean to remove unnecessary values from output
如果您只是登录控制台而没有使用
find()
方法,只需添加
console.log(success.toObject())
我也通过引用尝试过,但没有成功。是的,我重新安装了操作系统,所有东西都完全安装好了。更新猫鼬解决了我的问题。