Javascript MongooseJS在填充后返回空数组
我正在编写一个应用程序,使用Node.js和MongooseJS作为处理数据库调用的中间件 我的问题是,我有一些嵌套模式,其中一个填充方式错误。当我跟踪人口的每一步时,除了Javascript MongooseJS在填充后返回空数组,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,我正在编写一个应用程序,使用Node.js和MongooseJS作为处理数据库调用的中间件 我的问题是,我有一些嵌套模式,其中一个填充方式错误。当我跟踪人口的每一步时,除了设备数组是空的之外,所有数据都很好。我仔细检查了数据库,发现数组中有数据,所以应该没问题 我有Roomschema。Room的每个对象都有一个名为DeviceGroups的字段。此字段包含一些信息,其中一个是名为Devices的数组,其中存储了分配给父房间的设备 正如您在代码中看到的,我正在根据服务器请求中的房间ID查找房间。
设备
数组是空的之外,所有数据都很好。我仔细检查了数据库,发现数组中有数据,所以应该没问题
我有Room
schema。Room
的每个对象都有一个名为DeviceGroups
的字段。此字段包含一些信息,其中一个是名为Devices
的数组,其中存储了分配给父房间的设备
正如您在代码中看到的,我正在根据服务器请求中的房间ID查找房间。所有内容都填充良好,数据与数据库中的数据一致。问题是设备
数组为空
这是MongooseJS的一种怪癖,还是我在这里做错了什么,devices
array返回为空?我检查了数据库本身,里面有一些数据,所以数据很好,错误在粘贴的代码中的某个地方
守则:
模式:
const roomSchema = Schema({
name: {
type: String,
required: [true, 'Room name not provided']
},
deviceGroups: [{
type: Schema.Types.ObjectId,
ref: 'DeviceGroup'
}]
}, { collection: 'rooms' });
const deviceGroupSchema = Schema({
parentRoomId: {
type: Schema.Types.ObjectId,
ref: 'Room'
},
groupType: {
type: String,
enum: ['LIGHTS', 'BLINDS', 'ALARM_SENSORS', 'WEATHER_SENSORS']
},
devices: [
{
type: Schema.Types.ObjectId,
ref: 'LightBulb'
},
{
type: Schema.Types.ObjectId,
ref: 'Blind'
}
]
}, { collection: 'deviceGroups' });
const lightBulbSchema = Schema({
name: String,
isPoweredOn: Boolean,
currentColor: Number
}, { collection: 'lightBulbs' });
const blindSchema = Schema({
name: String,
goingUp: Boolean,
goingDown: Boolean
}, { collection: 'blinds' });
数据库调用:
Room
.findOne({ _id: req.params.roomId })
.populate({
path: 'deviceGroups',
populate: {
path: 'devices'
}
})
.lean()
.exec(function(err, room) {
if (err) {
res.send(err);
} else {
room.deviceGroups.map(function(currentDeviceGroup, index) {
if (currentDeviceGroup.groupType === "BLINDS") {
var blinds = room.deviceGroups[index].devices.map(function(currentBlind) {
return {
_id: currentBlind._id,
name: currentBlind.name,
goingUp: currentBlind.goingUp,
goingDown: currentBlind.goingDown
}
});
res.send(blinds);
}
});
}
})
你能删除你的其他语句中的所有内容吗
console.log(room)
检查mongo存储以确保您的集合中有数据。这是一个使用方法在单个数组中使用多个架构的示例
const roomSchema = Schema({
name: {
type: String,
required: [true, 'Room name not provided']
},
deviceGroups: [{ type: Schema.Types.ObjectId, ref: 'DeviceGroup' }]
});
const deviceGroupSchema = Schema({
parentRoom: { type: Schema.Types.ObjectId, ref: 'Room' },
groupType: {
type: String,
enum: ['LIGHTS', 'BLINDS', 'ALARM_SENSORS', 'WEATHER_SENSORS']
},
devices: [{ type: Schema.Types.ObjectId, ref: 'Device' }]
});
// base schema for all devices
function DeviceSchema() {
Schema.apply(this, arguments);
// add common props for all devices
this.add({
name: String
});
}
util.inherits(DeviceSchema, Schema);
var deviceSchema = new DeviceSchema();
var lightBulbSchema = new DeviceSchema({
// add props specific to lightBulbs
isPoweredOn: Boolean,
currentColor: Number
});
var blindSchema = new DeviceSchema({
// add props specific to blinds
goingUp: Boolean,
goingDown: Boolean
});
var Room = mongoose.model("Room", roomSchema );
var DeviceGroup = mongoose.model("DeviceGroup", deviceGroupSchema );
var Device = mongoose.model("Device", deviceSchema );
var LightBulb = Device.discriminator("LightBulb", lightBulbSchema );
var Blind = Device.discriminator("Blind", blindSchema );
// this should return all devices
Device.find()
// this should return all devices that are LightBulbs
LightBulb.find()
// this should return all devices that are Blinds
Blind.find()
在该集合中,您将看到每个设备上的\u\t
属性
使用符合所用模式的值(灯泡或盲板)
我没有尝试过该代码,也有一段时间没有使用mongoose,但我希望它能起作用:)
更新-测试工作示例
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var util = require('util');
const roomSchema = Schema({
name: {
type: String,
required: [true, 'Room name not provided']
},
deviceGroups: [{ type: Schema.Types.ObjectId, ref: 'DeviceGroup' }]
});
const deviceGroupSchema = Schema({
parentRoomId: { type: Schema.Types.ObjectId, ref: 'Room' },
groupType: {
type: String,
enum: ['LIGHTS', 'BLINDS', 'ALARM_SENSORS', 'WEATHER_SENSORS']
},
devices: [{ type: Schema.Types.ObjectId, ref: 'Device' }]
});
// base schema for all devices
function DeviceSchema() {
Schema.apply(this, arguments);
// add common props for all devices
this.add({
name: String
});
}
util.inherits(DeviceSchema, Schema);
var deviceSchema = new DeviceSchema();
var lightBulbSchema = new DeviceSchema({
// add props specific to lightBulbs
isPoweredOn: Boolean,
currentColor: Number
});
var blindSchema = new DeviceSchema();
blindSchema.add({
// add props specific to blinds
goingUp: Boolean,
goingDown: Boolean
});
var Room = mongoose.model("Room", roomSchema );
var DeviceGroup = mongoose.model("DeviceGroup", deviceGroupSchema );
var Device = mongoose.model("Device", deviceSchema );
var LightBulb = Device.discriminator("LightBulb", lightBulbSchema );
var Blind = Device.discriminator("Blind", blindSchema );
var conn = mongoose.connect('mongodb://127.0.0.1/test', { useMongoClient: true });
conn.then(function(db){
var room = new Room({
name: 'Kitchen'
});
var devgroup = new DeviceGroup({
parentRoom: room._id,
groupType: 'LIGHTS'
});
var blind = new Blind({
name: 'blind1',
goingUp: false,
goingDown: true
});
blind.save();
var light = new LightBulb({
name: 'light1',
isPoweredOn: false,
currentColor: true
});
light.save();
devgroup.devices.push(blind._id);
devgroup.devices.push(light._id);
devgroup.save();
room.deviceGroups.push(devgroup._id);
room.save(function(err){
console.log(err);
});
// Room
// .find()
// .populate({
// path: 'deviceGroups',
// populate: {
// path: 'devices'
// }
// })
// .then(function(result){
// console.log(JSON.stringify(result, null, 4));
// });
}).catch(function(err){
});
我认为您定义
设备的方式不正确。在一个数组中有多个模式引用的一种方法是使用see this@Molda。在我的例子中,你能提供一个简单的例子吗?当然,discriminator
正是我想要的,但是你链接的另一篇文章有点复杂,因为它使用了所有的中间件,对我来说不是很清楚。你能举个例子说明如何在我的案例中使用它吗?谢谢,我在发帖之前已经做过了。正如我在帖子中所写的——我检查了数据库,所有的id引用都保存了,我记录了room
,除了devices
array(它是空的)之外,所有的东西都被正确填充了。你建议调用函数util.inherits(DeviceSchema,Schema)代码>。我的问题是:什么是util
?我没有看到任何地方申报。它像是NPM的另一个软件包还是mongoose本身的东西?我用了你的解决方案,我仍然得到一个空数组。我检查了数据库,数据保存在那里。这是输出:[{u id:undefined,name:undefined,goingUp:undefined,goingDown:undefined}]util
是内置模块,只需要它var util=require('util')
谢谢!你提供的例子很有用!另外,它帮助我更好地理解了鉴别器
和猫鼬
本身。我仍然需要在鉴别器
上做一些工作,但是,多亏了你,我离你更近了!我很高兴能帮上忙。我想再指出一点。使用填充devicegroup和devices的Room.find调用将对db进行3次单独调用,因为它需要查询3次集合。如果可能的话,我一定会尽量避免使用设备组来简化它。这只是我的意见,但必须牢记在心。