Javascript 如何处理sailsjs中的嵌入式json

Javascript 如何处理sailsjs中的嵌入式json,javascript,express,sails.js,Javascript,Express,Sails.js,有没有办法从sails.js控制器中嵌入的json创建相关对象 在正在创建的类的控件中,我需要在不同的类中创建一个相关记录,并包含刚刚创建的父记录的id “子”记录的数据嵌入在创建父记录的json中。正在使用的数据库是Mongo,因此它不强制执行关系 如何创建这些相关对象 任何可供参考的例子都将不胜感激 更新: 我已经实现了第一个答案中的更改,现在得到错误“[TypeError:Cannotcallmethod'all'of undefined]” 有什么想法吗 编辑以添加代码: 采购信息模型

有没有办法从sails.js控制器中嵌入的json创建相关对象

在正在创建的类的控件中,我需要在不同的类中创建一个相关记录,并包含刚刚创建的父记录的id

“子”记录的数据嵌入在创建父记录的json中。正在使用的数据库是Mongo,因此它不强制执行关系

如何创建这些相关对象

任何可供参考的例子都将不胜感激

更新:

我已经实现了第一个答案中的更改,现在得到错误“[TypeError:Cannotcallmethod'all'of undefined]”

有什么想法吗

编辑以添加代码:

采购信息模型

module.exports = {

attributes: {

parent_account:{
    type:'string'
},

  purchase_date:{
      type:'datetime'
  },

  product:{
      type:'string'
  },

  subscription_length:{
      type:'integer'
  },

  renewal_date:{
      type:'date'
  },

  users_authorized:{
      type:'integer'
  },

  users_activated:{
      type:'integer'
  },

  company_name:{
      type:'string'
  },

  company_contact:{
      type:'string'
  },

  company_email:{
      type:'string',
      required:true,
      email:true
  },

  company_phone:{
      type:'string'
  },

  intended_use:{
      type:'string'
  },

  // reference to company
  company:{
      collection: 'company',
      via:'purchaser'
  }
}

};
公司模式

module.exports = {


attributes: {

company_name:{
    type:'string',
    required:'true'
},

  main_addr1:{
      type:'string',
      required:'true'
  },

  main_addr2:{
      type:'string',
      required:'true'
  },

  main_city:{
      type:'string',
      required:'true'
  },

  main_state:{
      type:'string',
      required:'true'
  },

  main_zip:{
      type:'string',
      required:'true'
  },

  main_country:{
      type:'string',
      required:'true'
  },

  mailing_same_as_main:{
      type:'boolean'
  },

  mailing_addr1:{
      type:'string'
  },

  mailing_addr2:{
      type:'string'
  },

  mailing_city:{
      type:'string'
  },

  mailing_state:{
      type:'string'
  },

  mailing_zip:{
      type:'string'
  },

  mailing_country:{
      type:'string'
  },

  primary_contact:{
      type:'string'
  },

  company_email:{
      type:'string',
      required:true,
      email:true
  },

  purchaser: {
      model: 'purchaseinfo'
  }

}

};
采购信息控制器

module.exports = {

  create: function(res, req){
  var purchaseData = req.params.all();

  // Extract the company info from the POSTed data
  var companyData = purchaseData.company_info;

 // Create the new Purchase record
  PurchaseInfo.create(purchaseData).exec(function(err,newPurchaseInfo){
      if (err){return res.req.serverError(err);}

      // Create the new Company, linking it to the purchase
      companyData.purchaser = newPurchaseInfo.id;
      Company.create(companyData).exec(function(err, newCompany) {
          if (err) {return res.serverError(err);}
          return res.json(newPurchaseInfo);
      })
  })
},
_config: {}
};
职位


Sails v0.10允许您在模型之间创建关联。下面是一个关于
用户
宠物
型号的快速示例:

/* api/models/User.js */
module.exports = {
   attributes: {
     name: 'string',
     pets: {
        collection: 'pet',
        via: 'owner'
     }
   }
};




此代码包括对用户的默认
create
blueprint操作的覆盖;您还可以为其创建单独的控制器操作。事实上,你可能还想检查宠物是否已经存在,做其他验证,等等。但这应该会让你上路

斯科特,谢谢你的回答!我试过了,但还没能成功。我一直在有效地“无法读取未定义的宠物”。因此,身体似乎没有被解析。此外,我在“serverError”引用中也会出现错误。这些是帆的还是你的功能?再次感谢你的帮助!抱歉,请编辑答案以指定这一切都在最新版本的Sails中,您可以获取或通过
npm安装sails@beta
。虽然
res.serverError
应该可以在v0.9.x中工作,但也将req.body更改为req.params.all(),这会对发送到路由的所有参数进行哈希处理。太棒了!今晚回家后我会查一查。再次感谢!在斯科特的帮助下,我成功地完成了这项工作!谢谢你,斯科特。
/* api/models/User.js */
module.exports = {
   attributes: {
     name: 'string',
     pets: {
        collection: 'pet',
        via: 'owner'
     }
   }
};
/* api/models/Pet.js */
module.exports = {
   attributes: {
     name: 'string',
     breed: 'string',
     owner: {
        model: 'user'
     }
   }
};
/* api/controllers/UserController.js */
module.exports = {

  create: function(req, res) {

    // Assuming the POST contains all the data we need to create a user...
    var userData = req.params.all();
    // Extract the pet from the POSTed data
    var petData = userData.pet;
    // Delete the pet from the POSTed data, if you
    // don't want it saved to the User collection in Mongo
    delete userData.pet;
    // Create the new user
    User.create(userData).exec(function(err, newUser) {
       if (err) {return res.serverError(err);}
       // Create the new pet, linking it to the user
       petData.owner = newUser.id;
       Pet.create(petData).exec(function(err, newPet) {
          if (err) {return res.serverError(err);}
          return res.json(newUser);
       });

    });

  }
};