json - compoundJS::如何创建模式来反射(reflect)半复杂的 JSON 对象?

标签 json node.js mongodb compoundjs jugglingdb

如果我的架构如下:

var Configuration = describe('Configuration', function () {
     property('name', String);
     set('restPath', pathTo.configurations);
 });

var Webservice = describe('Webservice', function () {
     property('wsid', String);
     property('url', String);
 });
 Configuration.hasMany(Webservice, { as: 'webservices', foreignKey: 'cid'});

这反射(reflect)了这样的数据:

var configurationJson = {
    "name": "SafeHouseConfiguration2",
    "webServices": [
        {"wsid": "mainSectionWs", "url":"/apiAccess/getMainSection" },
        {"wsid": "servicesSectionWs", "url":"/apiAccess/getServiceSection" }
    ]
};

我是否应该能够使用以下内容为 mongoDB 提供种子:

var configSeed = configurationJson;
Configuration.create(configSeed, function (err, configuration) {

)};

这将使用 json 对象中的数据创建下表:

  1. 配置
  2. 网络服务

最佳答案

由于这没有按我的预期工作,我的种子文件最终如下:

/db/seeds/development/Configuration.js

var configurationJson = {
    "name": "SafeHouseConfiguration2",
    "webServices": [
        {"wsid": "mainSectionWs", "url":"/apiAccess/getMainSection" },
        {"wsid": "servicesSectionWs", "url":"/apiAccess/getServiceSection" }
    ]
};

Configuration.create(configurationJson, function (err, configuration) {
    //Webservices config
   var webservicesConfig = configSeed.webServices;
   webservicesConfig.forEach(function(wsConfig){
     configuration.webservices.create(wsConfig, function(err){

     });
});

关于json - compoundJS::如何创建模式来反射(reflect)半复杂的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712678/

相关文章:

sql - 如何消除 DB2400 或任何 SQL 中 JSON_ARRAYAGG 中的重复行?

javascript - 带有 sourceComments : 'map' produces 'Assertion failed' 的 gulp 中的 SASS 任务

javascript - 在javascript中将整数转换为数字的小数部分最有效的方法是什么?

mongodb - 删除 Mongo 中的 STRANGE 集合

node.js - 当值嵌套在数组中时如何区分(计数)值?

json - 为什么 json.Unmarshal 使用引用而不使用指针?

php - 使用来自 Android 应用程序的带有 WHERE 子句的 JSON 获取对象数组

javascript - 在对象上设置属性时调用函数

java - 在 MongoDB 3.2 中创建索引以避免重复的文档/行

c# - json 与 xamarin android 解析