1

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

样本数据

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

谁能解释为什么没有插入这个嵌套对象以及修复它需要什么?

谢谢

4

1 回答 1

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);
    });
}
于 2015-11-06T13:19:11.370 回答