Мангуста не заселяет массив - PullRequest
0 голосов
/ 25 августа 2018

Я боролся с функцией mongoose.model.populate в течение нескольких часов.Я даже безуспешно пытался напрямую скопировать и вставить несколько решений.

У меня есть модель User, которая должна содержать массив созданных им «Дилемм», но я не смог ее заполнить..

Здесь представлены модели, а также реализация 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");

Дилемма.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 Response

[
    {
        "_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
}

Ответы [ 3 ]

0 голосов
/ 25 августа 2018

Вы проверили документацию здесь?

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

Это показывает аналогичную настройку (с авторами и историями.) Здесь упоминается "проталкивание" историй, чтобы иметь возможность использовать find / populate комбо.

0 голосов
/ 12 июня 2019

Я только что столкнулся с подобной проблемой сам.Заполнение ссылки сработало, но заполнение массива ссылок не сработало.Мне удалось заставить заполненный массив работать, явно указав имя модели в вызове заполнения, например:

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

Я не знаю, почему это имеет значение, когда имя упоминаемой моделиуже указано в схеме.

0 голосов
/ 25 августа 2018

Вы пробовали это?

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