Meteor SimpleSchema插入嵌套对象

时间:2015-11-06 12:05:16

标签: mongodb meteor simple-schema

我有以下集合,它有一个嵌套对象address,它使用Collection2 Meteor Package定义。我无法插入此嵌套对象的数据...

示例数据

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集合是空的地址?我尝试过很多东西,包括在初始插入后进行更新(这远非理想)。

任何人都可以解释为什么这个嵌套对象没有被插入以及修复它需要什么?

由于

1 个答案:

答案 0 :(得分:1)

使用 publish-counts 包订阅流星计数:

// server: publish the current size of a collection
Meteor.publish('getLocationCounts', function() {
    Counts.publish(this, 'locations-counter', ClassLocation.find());
});

一旦您订阅了“getLocationCounts”,就可以调用Counts.get('locations-counter')来反复获取计数器的值。

此函数将始终返回一个整数,如果计数器既未发布也未订阅,则返回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);
    });
}