【问题标题】:mongoose find() returns properties of modelmongoose find() 返回模型的属性
【发布时间】:2019-08-16 11:12:42
【问题描述】:

我正在尝试使用 mongoose 查找现有用户 我的查询是

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 } ]

我不知道是什么导致了这个问题?直到昨天它只返回用户数据,但这对我来说很奇怪。如何解决这个问题?有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    我也面临同样的问题,问题是如果您使用最新版本的 mongo 和旧的 mongoose ,可以通过安装较新的 mongoose 版本并运行 mongoose find() 来解决,这将解决您的问题。

    【讨论】:

    • 是的,我完全重新安装了我的操作系统和所有东西。更新猫鼬解决了我的问题。
    【解决方案2】:

    您只需在查询结束时使用lea() 即可摆脱这种情况

    const courses = await Course
        .find({ isPublished: true })
        .or([
            { price: { $gte: 15 } },
            { name: /.*by.*/i }
        ]).lean(); // use lean to remove unnecessary values from output 
    

    【讨论】:

      【解决方案3】:

      您最初获得的额外属性是由于结果是模型实例的集合,这些实例带有普通对象不可用的额外属性和方法。 您可以将 {lean: true} 作为选项传递,以获取普通对象,而无需所有这些额外的属性和方法。

      UserAccount.find(variables, {lean: true}, function (error, success) {
                  if (error) {
                      response.send({status: false});
                  } else {
                      console.log(success);
                  }
              }); 
      

      【讨论】:

      猜你喜欢
      • 2018-05-18
      • 2015-09-21
      • 2015-04-11
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多