Mongoosejs .find返回整个模型而不是文档 [英] Mongoosejs .find returning whole model instead of document

查看:189
本文介绍了Mongoosejs .find返回整个模型而不是文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在现有模型上的.find()查询上运行.我过去使用过此代码,但没有进行任何更改,但是现在突然由于某种原因它不起作用.我在想MongoDB或MongooseJS已更新,功能已更改.

I am running on .find() query on an existing model. I have used this code in the past and have changed nothing but now all of the sudden it's not working for some reason. I am thinking either MongoDB or MongooseJS updated and the functionality has changed.

var retrieve = function() {
  Repo.find({}, function(err, docs) {
    console.log(docs)
  })
};

retrieve();

返回

[
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: {},
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5e02e91c908f0f086e737189,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      __v: 0,
      stars: 2,
      id: 1322,
      url: 'url',
      name: 'name',
      _id: 5e02e91c908f0f086e737189
    },
    '$init': true
  },
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: {},
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5e02e92c3f6b72088246c563,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      __v: 0,
      stars: 2,
      id: 2,
      url: 'url1',
      name: 'name1',
      _id: 5e02e92c3f6b72088246c563
    },
    '$init': true
  }
]

它应该返回

[{name: 'name', id: 2, url: 'url', stars: 2},
{name: 'name1', id: 1322, url: 'url1', stars: 2}]

我不知道为什么会这样

----为Ahsok编辑- 我尝试使用您的代码

---- edit for Ahsok --- I tried using your code

const retrieve = () => {
  Repo.find({})
  .then(repo => {
    console.log({ repo })
  })
  .catch(error => {
    console.log({ error })
  })
};

它仍然没有返回需要的状态.现在回来了

And it's still not returning what it needs to be. Now it's returning

{
  repo: [
    model {
      '$__': [InternalCache],
      isNew: false,
      errors: undefined,
      _doc: [Object],
      '$init': true
    },
    model {
      '$__': [InternalCache],
      isNew: false,
      errors: undefined,
      _doc: [Object],
      '$init': true
    }
  ]
}

与上面返回的内容相同,只是格式略有不同

Which is the same thing it was returning above, just in a slightly different format

推荐答案

如果您使用的是异步函数,请使用此语法

If you are using async function then use this syntax

const retrieve = async () => {
  const repo = await Repo.find({})
  console.log(repo)
};

如果您不知道发生了什么,请使用此语法

If you have no idea whats going on up there use this syntax

const retrieve = () => {
  Repo.find({})
  .then(repo => {
    console.log({ repo })
  })
  .catch(error => {
    console.log({ error })
  })
};

您也可以从此处进行文档下载.

You can take and doc ride from here too.

为什么会发生这种情况,因为find返回游标或Promise从中检索_doc,所以您需要使用Promise. 在这里,第一种类型的解决方案很流行清理代码.

Why this happens Because find return cursor or promise to retrieve _doc from it you need to use the promise. Here the first type solution is popular to clean code.

这篇关于Mongoosejs .find返回整个模型而不是文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆