【问题标题】:Mongoose not returning full dataset猫鼬没有返回完整的数据集
【发布时间】:2016-11-14 17:59:22
【问题描述】:

我有一个这样设置的模型:

var Post = mongoose.Schema({

      "_uid": String,
      "post_title": String,
      "post_content": String,
      "post_date": Date,
      "user": String,
      "slug": String,
      "attached_media": [[
        {
          "format": String,
          "type": String,
          "small": String,
          "medium": String,
          "large": String
        }
      ]],
      "likes_count": Number,
      "likes": [
        {....

其中的关键部分是 attach_media 数组,因为每当我将 .find({}) 命令与 mongoose 一起使用时,都不会返回包含任何数据的数组元素。 数据肯定在其中,因为当我使用 mongo 命令 shell 时,我看到数组已填充。

我的查找方法:

Post.statics.getAll = function getAll(next){
  this.find({})
  .sort({'post_date':'desc'})
  .exec(function(err, doc){
    if(err) console.log(err)
    next(null, doc)
  })
}

猫鼬归来:

[ { attached_media: [],
    likes: [],
    comments: [],
    __v: 0,
    _uid: '577e67d2a4387d0b1b480e2c',
    user: 'Aaron Griffin',
    post_title: 'This should be a couple of images',
    post_date: Tue Jul 12 2016 13:40:06 GMT+0000 (UTC),
    post_content: '',
    _id: 5784f33681adc21c121a94d1 },
  { attached_media: [],
    likes: [],
    comments: [],
    __v: 0,
    _uid: '577e67d2a4387d0b1b480e2c',
    user: 'Aaron Griffin',
    post_title: 'Hello',
    post_date: Tue Jul 12 2016 13:34:03 GMT+0000 (UTC),
    post_content: '',
    _id: 5784f1cb81adc21c121a94d0 } ]

但应该返回(mongo shell 返回的内容):

{ "_id" : ObjectId("5784f33681adc21c121a94d1"), "post_content" : "", "post_date" : ISODate("2016-07-12T13:40:06.486Z"), "post_title" : "This should be a couple of images", "user" : "Aaron Griffin", "_uid" : "577e67d2a4387d0b1b480e2c", "comments" : [ ], "likes" : [ ], "attached_media" : [ { "large" : "large_f087dd83f697e3742d249e45d47f102b.jpg", "medium" : "medium_f087dd83f697e3742d249e45d47f102b.jpg", "small" : "small_f087dd83f697e3742d249e45d47f102b.jpg", "type" : ".jpg", "format" : "image/jpeg" }, { "large" : "large_fe6dfb7bfcff38883badb800d049dc19.jpg", "medium" : "medium_fe6dfb7bfcff38883badb800d049dc19.jpg", "small" : "small_fe6dfb7bfcff38883badb800d049dc19.jpg", "type" : ".jpg", "format" : "image/jpeg" }, { "large" : "large_527b0b9eb14f4310e535eae2c6e48f4a.jpg", "medium" : "medium_527b0b9eb14f4310e535eae2c6e48f4a.jpg", "small" : "small_527b0b9eb14f4310e535eae2c6e48f4a.jpg", "type" : ".jpg", "format" : "image/jpeg" } ], "__v" : 0 }
{ "_id" : ObjectId("5784f1cb81adc21c121a94d0"), "post_content" : "", "post_date" : ISODate("2016-07-12T13:34:03.848Z"), "post_title" : "Hello", "user" : "Aaron Griffin", "_uid" : "577e67d2a4387d0b1b480e2c", "comments" : [ ], "likes" : [ ], "attached_media" : [ { "large" : "large_c869348bfc5f12f4099a0f6e2d8941ae.jpg", "medium" : "medium_c869348bfc5f12f4099a0f6e2d8941ae.jpg", "small" : "small_c869348bfc5f12f4099a0f6e2d8941ae.jpg", "type" : ".jpg", "format" : "image/jpeg" } ], "__v" : 0 }

运行和记录查询的代码:

router.get('/', function(req, res, next) {
  if (req.user) {
    if(req.user.registration_level == 1){
      res.redirect('/signup/complete')
    }
    Post.getAll(function(err, posts){
      // console.log(req.user)
      console.log(posts)
      res.render('dashboard', { title: 'Member feed' , posts: posts, user: req.user });
    })
  } else {
      res.render('index.ejs', { title: 'Express' });
  }
});

如果有人知道为什么这不起作用,我将不胜感激:)

【问题讨论】:

  • 您可以发布您的find() 查询吗?
  • Post.statics.getAll = function getAll(next){ this.find({}) .sort({'post_date':'desc'}) .exec(function(err, doc){ if(err) console.log(err) next(null, doc) }) }
  • 你在哪里 console.log ?你在哪里记录你所说的猫鼬return.s?
  • 见上面的编辑:)
  • 对不起,我一无所知。我唯一可以反对的是,确保你重新填充。祝你好运

标签: node.js mongodb express mongoose


【解决方案1】:

我不确定,但你在那里使用了数组数组...你能用这样的一级数组替换它吗?

var Post = mongoose.Schema({

  "_uid": String,
  "post_title": String,
  "post_content": String,
  "post_date": Date,
  "user": String,
  "slug": String,
  "attached_media": [
    {
      "format": String,
      "type": String,
      "small": String,
      "medium": String,
      "large": String
    }
  ],
  "likes_count": Number,
  "likes": [
    {....

【讨论】:

  • 刚刚改了,还是没有返回完整的文档
  • 您是否尝试过创建新数据。因为旧数据现在是错误的。 db.posts.remove( { }。如果可行,请不要忘记接受答案。如果不行,请不要投反对票。
  • 刚刚做了,还是没有运气:POST /login 302 152.107 ms - 46 [ { attached_media: [], likes: [], comments: [], __v: 0, _uid: '577e67d2a4387d0b1b480e2c', user: 'Aaron Griffin', post_title: 'This is new', post_date: Tue Jul 12 2016 13:23:37 GMT+0000 (UTC), post_content: '', _id: 5784ef59193c088c0d0afaf4 },...
  • prntscr.com/brya5l prntscr.com/bryaq9
  • 请用所有相关代码和文件更新您的问题。包括您提交的方式和您列出的方式。
【解决方案2】:

我想通了!

我需要使用 Mongoose 提供的子文档:

var mediaSchema = new mongoose.Schema({
  "format": String,
  "type": String,
  "small": String,
  "medium": String,
  "large": String
})

var Post = mongoose.Schema({
  "_uid": String,
  "post_title": String,
  "post_content": String,
  "post_date": Date,
  "user": String,
  "slug": String,
  "attached_media": [mediaSchema],
  "likes_count": Number,
  "likes": [
...

感谢大家的帮助:)

【讨论】:

    猜你喜欢
    • 2015-04-07
    • 2018-07-12
    • 2020-07-21
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2021-05-25
    • 2015-08-05
    • 2016-04-30
    相关资源
    最近更新 更多