簡體   English   中英

貓鼬填充不填充數組

[英]Mongoose populate does not populate array

我已經在mongoose.model.populate函數上苦苦掙扎了幾個小時。 我什至嘗試直接復制和粘貼幾種解決方案而沒有運氣。

我有一個用戶模型,該模型應該包含他/她創建的“困境”數組,但是我無法填充它。

這是模型以及populate()的實現。

User.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

// Create Schema
const UserSchema = new Schema({
  username: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  },
  dilemmas: [
    {
      type: Schema.Types.ObjectId,
      ref: "Dilemma"
    }
  ]
});

module.exports = User = mongoose.model("User", UserSchema, "users");

Dilemma.js

const mongoose = require("mongoose");
const slug = require("mongoose-slug-generator");
const Schema = mongoose.Schema;
mongoose.plugin(slug);

// Create Schema
const DilemmaSchema = new Schema({
  creator: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  title: {
    type: String
  },
  slug: {
    type: String,
    slug: "title"
  },
  red: {
    type: String,
    required: true
  },
  blue: {
    type: String,
    required: true
  },
  red_votes: {
    type: Number,
    default: 0,
    required: true
  },
  blue_votes: {
    type: Number,
    default: 0,
    required: true
  },
  likes: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: "User"
      }
    }
  ],
  comments: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: "User"
      },
      text: {
        type: String,
        required: true
      },
      author: {
        type: String
      },
      avatar: {
        type: String
      },
      date: {
        type: Date,
        default: Date.now
      }
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = Dilemma = mongoose.model("Dilemma", DilemmaSchema, "dilemmas");

Routes.js

// @route   GET api/users/profile
// @desc    Gets logged in user's profile
// @access  Private
router.get(
  "/profile",
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    User.find({ username: req.user.username })
      .populate("dilemmas")
      .then(user => {
        if (!user) {
          errors.nouser = "There is no user";
          return res.status(404).json(errors);
        }
        res.json(user);
      })
      .catch(err => res.status(400).json(err));
  }
);

JSON回應

[
    {
        "_id": "5b807beef770e7c7e6bf7ce0",
        "dilemmas": [],
        "username": "Jonas",
        "email": "Mohrdevelopment@gmail.com",
        "password": "$2a$10$QaqljS9x08YQ9N9EuCBTpO114ZJUFuVxAV80xMzImNi8eW2frPg0C",
        "date": "2018-08-24T21:43:10.411Z",
        "__v": 0
    }
]

JSON困境響應

[
    {
        "red_votes": 0,
        "blue_votes": 0,
        "_id": "5b80975f6e47fecba621f295",
        "user": "5b807beef770e7c7e6bf7ce0",
        "title": "Am i the real author asdsdasd?",
        "red": "This is the red dilemma",
        "blue": "This is the blue dilemma",
        "likes": [],
        "comments": [],
        "date": "2018-08-24T23:40:15.381Z",
        "slug": "am-i-the-real-author-asdsdasd",
        "__v": 0
    },
    {
        "red_votes": 0,
        "blue_votes": 0,
        "_id": "5b808e789bc36bcae8c6c3ad",
        "creator": "5b807beef770e7c7e6bf7ce0",
        "title": "Am i the real author?",
        "red": "This is the red dilemma",
        "blue": "This is the blue dilemma",
        "likes": [],
        "comments": [],
        "date": "2018-08-24T23:02:16.565Z",
        "slug": "am-i-the-real-author",
        "__v": 0
    }
]

JSON用戶響應

{
    "_id": {
        "$oid": "5b807beef770e7c7e6bf7ce0"
    },
    "dilemmas": [],
    "username": "Jonas",
    "email": "Mohrdevelopment@gmail.com",
    "password": "$2a$10$QaqljS9x08YQ9N9EuCBTpO114ZJUFuVxAV80xMzImNi8eW2frPg0C",
    "date": {
        "$date": "2018-08-24T21:43:10.411Z"
    },
    "__v": 0
}

你有嘗試過嗎?

User.find({ username: req.user.username })
  .populate("dilemmas")
  .exec() // <-- add exec() to perform the search
  .then(user => {
    ...
  })

您是否在此處檢查了文檔?

https://mongoosejs.com/docs/populate.html#refs-to-children

它顯示了類似的設置(包括“作者和故事”。)提到了“推送”故事,以便能夠使用“ find / populate組合。

我自己也遇到了類似的問題。 填充參考有效,但填充參考數組無效。 通過在填充調用中明確指定模型名稱,我能夠使數組填充正常工作,例如:

User.find({ ... }).populate({
  path: 'dilemmas',
  model: 'Dilemma',
});

當模式中已經指定了引用模型的名稱時,我不知道為什么這會有所作為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM