简体   繁体   English

流星SimpleSchema插入嵌套对象

[英]Meteor SimpleSchema insert with nested object

I have the following collection which has a nested object address which is defined using the Collection2 Meteor Package. 我有以下具有嵌套对象address集合,该对象address是使用Collection2流星包定义的。 I am unable to insert the data for this nested object... 我无法为此嵌套对象插入数据...

Sample data 样本数据

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
}];

Collection definition 集合定义

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);

Routine 常规

if(ClassLocation.find().count() === 0) {
  _.each(locations, function (location) {
    console.log(location.address);
    ClassLocation.insert(location);
  });
}

The Problem 问题

The console logs out the location address details object fine but, my MongoDb collection of inserted documents is empty for address? 控制台可以很好地注销位置地址详细信息对象,但是我插入的文档的MongoDb集合的地址是否为空? I've tried a multitude of things, including doing an update after the initial insert (which is far from ideal). 我尝试了很多事情,包括在初始插入之后进行更新(这远非理想)。

Can anyone explain why this nested object isn't being inserted and what is required to fix it? 谁能解释为什么未插入此嵌套对象以及修复该嵌套对象需要什么?

Thanks 谢谢

Use the publish-counts package to subscribe a count in meteor: 使用publish-counts包订阅流星计数:

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

Once you've subscribed to 'getLocationCounts', you can call Counts.get('locations-counter') to get the value of the counter, reactively. 订阅“ getLocationCounts”后,您可以调用Counts.get('locations-counter')以被动方式获取计数器的值。

This function will always return an integer, 0 is returned if the counter is neither published nor subscribed to. 该函数将始终返回整数,如果计数器既未发布也未订阅,则返回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);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM