猫鼬人口不起作用

时间:2014-10-23 11:12:24

标签: node.js express mongoose

您好我有这个架构(称为schema.js):

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var RoomSchema = new Schema({
  name: { type: String, required: true, index: { unique: true } },
  people: { type: Number, required: true },
  childrens: {type: Number, required: true},
  total: {type: Number, required: true}
});

var Room = mongoose.model('Room', RoomSchema);

var AvSchema = new Schema({
  roomId:  {type: Schema.Types.ObjectId, ref: 'Room'},
  people: { type: Number, required: true },
  childrens: {type: Number, required: true},
  total: {type: Number, required: true}
});

var Av = mongoose.model('Av', AvSchema);

module.exports = {
  Room: Room,
  Av: Av
};

在我的路线文件中:

module.exports = function(app) {
  var model = require('../models/Schema');

  app.get('/api/rooms', function(req, res) {
    model.Room.find(function(err, rooms) {
      if (err)
        res.send(err);

      res.json(rooms);
    });
  });


  app.get('/api/av', function(req, res) {
    model.Av.find().populate('roomId').exec(function(err, av) {
      if (err)
        res.send(err);

      res.json(av);
    });
  });
};

db的图片: enter image description here

GET / api / rooms - 回复:

[{
  "_id": "5444d0dd9a31437167eea816",
  "name": "Single",
  "people": 1,
  "childrens": 1,
  "total": 4
}, {
  "_id": "5444d1009a31437167eea817",
  "name": "Double",
  "people": 2,
  "childrens": 2,
  "total": 10
}]

当我打电话给api /房间看起来很好但是当我打电话给api / av我有一个空阵列[] ....知道我做错了吗?我应该提一下,我已经为两个roomsID

在av集合中插入了记录

提前谢谢。

8 个答案:

答案 0 :(得分:13)

默认情况下,Mongoose会将模型名称复数化以显示集合的名称,因此Mongoose会查找avs集合而不是av

您可以通过将其作为第三个参数传递给model来显式设置集合名称:

var Av = mongoose.model('Av', AvSchema, 'av');

答案 1 :(得分:2)

因为这是查询最受欢迎的结果

  

mongoose populate not working

我会列出它不适合我的原因,即使它不是这个已经解决的问题的直接答案,希望它能帮助某人

我遇到的问题是我在select({..}中指定了字段,但没有指定我试图填充的字段。

答案 2 :(得分:1)

与CodyBugstein的答案类似,我正在发布为何它不适用于我的情况,即使它与OP的情况不同。

我试图在.post('save')钩子中填充架构的“ pro”字段,如下所示:

mySchema.post('save', function(doc, next) {
    console.log(doc.pro); // Expected to log ObjectID
    doc.populate("pro"); // Populate field
    console.log(doc.pro); // Expected to log actual pro document
}

但是,第二个console.log也在记录ObjectID 而不是文档。

在为此苦苦挣扎了一个小时并尝试了不同的方法之后,我发现我所要做的就是使用诺言并调用execPopulate(),以便它返回一个完整的诺言。我使用了async/await,但您也可以使用.then

mySchema.post('save', async function(doc, next) {
    console.log(doc.pro); // Expected to log ObjectID
    await doc.populate("pro").execPopulate(); // Populate field
    console.log(doc.pro); // Expected to log actual pro document
}

这样,第二个console.log确实记录了整个专业文档:)

答案 3 :(得分:0)

不要忘记将ref属性添加到您要填充的属性的架构中。例如

// ...
const orderSchema = new mongoose.Schema({
  userId: {
    type: Types.ObjectId,
    required: true
  },
  reservationId: {
    type: Types.ObjectId,
    required: true,
    ref: 'Reservation' // <-- don't forget the ref
  }
}, {
  timestamps: true
})
// ...

请参见Mongoose Populate

答案 4 :(得分:0)

我遇到了同样的问题,但是没有一个答案对我有用。

我想在查询文档后填充它。

这不起作用:

// IIFE for async/await
( async() => {

    var user = await User.findOne( { _id } );

    await user.populate( 'comments' ); // Doesn't work

} );

在没有回调的情况下调用.populate()时将不会执行的Mongoose Documentation explains。相反,您需要使用.populate().execPopulate()

// IIFE for async/await
( async() => {

    var user = await User.findOne( { _id } );

    await user.populate( 'comments' ).execPopulate(); // Works as expected

} );

答案 5 :(得分:0)

还要检查您的架构中没有将depopulate中的toJSONtoObject选项设置为true。 (我自己是facepalm)

See all schema options

答案 6 :(得分:0)

对我来说,这是因为数据不正确。我要填充的ID已从主表中删除。 因此,当我进行填充时,由于ID不在表中,因此没有填充填充的数据。

答案 7 :(得分:-2)

对我来说,问题在于我不需要在文件的开头填充模型。

相关问题