如何在Sails js中处理嵌入的json

How to handle embedded json in Sails js

本文关键字:json 处理 Sails js      更新时间:2023-09-26

是否有办法在sails.js控制器中从嵌入式json创建相关对象?

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

"子"记录的数据嵌入在创建父记录的json中。使用的数据库是Mongo,所以它不强制关系。

如何创建这些相关的对象?

如能举例说明,不胜感激。

更新:

我已经在第一个答案中实现了更改,现在得到错误"[TypeError: Cannot call method 'all' of undefined]"

任何想法?

编辑添加代码:

PurchaseInfo模型
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'
  }
}
};

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: {}
};

文章

http://localhost:1337/purchaseinfo/create
body:
{   "parent_account": 1,    "purchase_date": "2014-03-03",  "product": "C V1",  "subscription_length": 12,  "renewal_date": "2015-03-03",   "users_authorized": 10,     "users_activated": 1,   "company_name": "Bob's Trucking",   "company_contact": "Jane Smith",    "company_email":"jsmith@bobstrucking.com",  "company_phone": 5035129657,    "intended_use": "trucking",     "company_info": {       "company_name": "Bob's Trucking",       "main_addr1": "101 First Street",       "main_city": "Portland",        "main_state": "OR",         "main_zip": "97230",        "main_country": "USA",      "mailing_same_as_main":"true",      "primary_contact": "Jane Smith",        "company_email": "info@bobstrucking.com"    } }

Sails v0.10允许在模型之间创建关联。下面是UserPet模型的快速示例:

/* 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);
       });
    });
  }
};

这段代码包括覆盖默认的create蓝图动作为用户;您还可以为它创建一个单独的控制器动作。在现实中,您可能还想检查宠物是否已经存在,进行其他验证等等。但这样你就能上路了!