Teo*_*dor 12 mongoose node.js express
您好我有这个架构(称为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
};
Run Code Online (Sandbox Code Playgroud)
在我的路线文件中:
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);
});
});
};
Run Code Online (Sandbox Code Playgroud)
db的图片:

GET/api/rooms - 回复:
[{
"_id": "5444d0dd9a31437167eea816",
"name": "Single",
"people": 1,
"childrens": 1,
"total": 4
}, {
"_id": "5444d1009a31437167eea817",
"name": "Double",
"people": 2,
"childrens": 2,
"total": 10
}]
Run Code Online (Sandbox Code Playgroud)
当我打电话给api /房间看起来很好但是当我打电话给api/av我有一个空阵列[] ....任何想法我做错了什么?我应该提一下,我已经在两个roomsID的av集合中插入了记录
先感谢您.
Sno*_*all 17
我有同样的问题,但没有一个答案对我有用。
我想在查询后填充文档。
这不起作用:
// IIFE for async/await
( async() => {
var user = await User.findOne( { _id } );
await user.populate( 'comments' ); // Doesn't work
} );
Run Code Online (Sandbox Code Playgroud)
该猫鼬文档解释了在调用时.populate()没有回调就不会被执行。相反,您需要使用.populate().execPopulate():
// IIFE for async/await
( async() => {
var user = await User.findOne( { _id } );
await user.populate( 'comments' ).execPopulate(); // Works as expected
} );
Run Code Online (Sandbox Code Playgroud)
Joh*_*yHK 14
默认情况下,Mongoose将模型名称复数化以得出集合的名称,因此Mongoose正在查找avs集合而不是av.
您可以通过将其作为第三个参数传递给以下来显式设置集合名称model:
var Av = mongoose.model('Av', AvSchema, 'av');
Run Code Online (Sandbox Code Playgroud)
与 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
}
Run Code Online (Sandbox Code Playgroud)
但是,第二个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
}
Run Code Online (Sandbox Code Playgroud)
这样,第二个console.log确实按预期记录了整个专业文档:)
不要忘记将 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
})
// ...
Run Code Online (Sandbox Code Playgroud)
参见猫鼬填充
| 归档时间: |
|
| 查看次数: |
13475 次 |
| 最近记录: |