Meteor SimpleSchema 插入嵌套对象
Meteor SimpleSchema insert with nested object
我有以下集合,其中有一个嵌套对象 address
,它是使用 Collection2 Meteor 包定义的。我无法为这个嵌套对象插入数据...
示例数据
var addresses = [{
"venue": "Kirkstall Abbey",
"street": "Kirkstall Lane",
"city": "Leeds",
"county": "West Yorkshire",
"postcode": "LS6 3LF"
},{
"venue": "Headingley High School",
"street": "",
"city": "Leeds",
"county": "West Yorkshire",
"postcode": "LS6 7QR"
}];
var locations = [{
"name": "Kirkstall Abbey",
"address": addresses[0],
"image": null,
"active": true
},{
"name": "Headingley",
"address": addresses[1],
"image": null,
"active": true
}];
集合定义
ClassLocation = new Mongo.Collection("class-location");
Schemas.ClassLocation = new SimpleSchema({
name: {
type: String,
optional: true
},
address: {
type: Object
},
image: {
type: String,
optional: true
}
active: {
type: Boolean,
optional: true
}
});
ClassLocation.attachSchema(Schemas.ClassLocation);
日常
if(ClassLocation.find().count() === 0) {
_.each(locations, function (location) {
console.log(location.address);
ClassLocation.insert(location);
});
}
问题
控制台可以很好地注销位置地址详细信息对象,但是,我的 MongoDb 插入文档集合的地址是空的?我已经尝试了很多事情,包括在初始插入后进行更新(这远非理想)。
谁能解释为什么这个嵌套对象没有被插入以及需要什么来修复它?
谢谢
使用publish-counts包在meteor中订阅一个计数:
// server: publish the current size of a collection
Meteor.publish('getLocationCounts', function() {
Counts.publish(this, 'locations-counter', ClassLocation.find());
});
订阅 'getLocationCounts' 后,您可以调用 Counts.get('locations-counter')
以被动地获取计数器的值。
此函数将始终 return 一个整数,如果计数器既未发布也未订阅,则 return 为 0。
// client: get the count value reactively
var exists = (Counts.get('locations-counter')=== 0);
if(exists) {
_.each(locations, function (location) {
console.log(location.address);
ClassLocation.insert(location);
});
}
我有以下集合,其中有一个嵌套对象 address
,它是使用 Collection2 Meteor 包定义的。我无法为这个嵌套对象插入数据...
示例数据
var addresses = [{
"venue": "Kirkstall Abbey",
"street": "Kirkstall Lane",
"city": "Leeds",
"county": "West Yorkshire",
"postcode": "LS6 3LF"
},{
"venue": "Headingley High School",
"street": "",
"city": "Leeds",
"county": "West Yorkshire",
"postcode": "LS6 7QR"
}];
var locations = [{
"name": "Kirkstall Abbey",
"address": addresses[0],
"image": null,
"active": true
},{
"name": "Headingley",
"address": addresses[1],
"image": null,
"active": true
}];
集合定义
ClassLocation = new Mongo.Collection("class-location");
Schemas.ClassLocation = new SimpleSchema({
name: {
type: String,
optional: true
},
address: {
type: Object
},
image: {
type: String,
optional: true
}
active: {
type: Boolean,
optional: true
}
});
ClassLocation.attachSchema(Schemas.ClassLocation);
日常
if(ClassLocation.find().count() === 0) {
_.each(locations, function (location) {
console.log(location.address);
ClassLocation.insert(location);
});
}
问题
控制台可以很好地注销位置地址详细信息对象,但是,我的 MongoDb 插入文档集合的地址是空的?我已经尝试了很多事情,包括在初始插入后进行更新(这远非理想)。
谁能解释为什么这个嵌套对象没有被插入以及需要什么来修复它?
谢谢
使用publish-counts包在meteor中订阅一个计数:
// server: publish the current size of a collection
Meteor.publish('getLocationCounts', function() {
Counts.publish(this, 'locations-counter', ClassLocation.find());
});
订阅 'getLocationCounts' 后,您可以调用 Counts.get('locations-counter')
以被动地获取计数器的值。
此函数将始终 return 一个整数,如果计数器既未发布也未订阅,则 return 为 0。
// client: get the count value reactively
var exists = (Counts.get('locations-counter')=== 0);
if(exists) {
_.each(locations, function (location) {
console.log(location.address);
ClassLocation.insert(location);
});
}