2015-12-23 10 views
0

Я строю приложение, используя SailsJS, AngularJS и MongoDB, я смущен, как вставить массив данных в объект. Это пример моя схемаКак вставить массив в объект?

Asset.js

module.exports = { 
    schema : false, 
    attributes: { 
     product : { model : 'Product' }, 
     store : { model : 'Store' }, 
     key  : { type : 'object' } 
    } 
}; 

Это моя точка зрения формы

<tbody ng-repeat="attr in product.category.templateAttribute" ng-show="{{product.category.name == 'custom'}}"> 
    <tr> 
     <td> 
      <input class="form-control input-small" value="{{attr.attribute}}" /> 
     </td> 
     <td> 
      <input class="form-control input-small" placeholder="name" ng-model="product.attributes[attr.attribute].name" /> 
     </td> 
     <td> 
      <input class="form-control input-small" placeholder="add price" ng-model="product.attributes[attr.attribute].additionalPrice" /> 
     </td> 
    </tr> 
    <tr> 
     <td colspan="3"> 
      <file-uploader class="form-control input-small" 
       max-size="25000" 
       result-model="product.attributes[attr.attribute].file"> 
      </file-uploader> 
     </td> 
    </tr> 
</tbody> 

Это мой запрос

ProductCustomAsset.create({ 
    product  : product.id, 
    store  : product.store, 
    key   : product.attributes 
}) 
.then(function(){ 
    next(); 
}) 
.catch(function(error){ 
    next(error); 
}); 

Что такое проблема в мой запрос, нет ошибки, но значение в ключе только массив нет объекта.

Я хочу привести быть этой

{ 
    "_id" : 5, 
    "product" : 3, 
    "store" : 2 
    "key": [ 
     { 
      "attribute": "xxxxxxx", 
      "name": "book", 
      "additionaPrice": "xxxx", 
      "file": "xxxx" , 
     } 
    ] 
} 
+1

Ваш запрос должен искать, если существует продукт User.findOne ("product"). Exec (function (err, product) { product.key.push ({/ * whatever * /}); product.save (function (err) {/ * все сделано * /}); }); http://stackoverflow.com/questions/18161056/push-values-into-array-of-mongodb-database-through-sails-js-waterline –

ответ

0

Из моего личного опыта с AngularJS, почему вы ссылками входов нг-модель с product.attributes?

Вы пробовали это:

<input class="form-control input-small" placeholder="name" ng-model="{{attr.name}}" /> 

как вы сделали:

<input class="form-control input-small" value="{{attr.attribute}}" /> 

И конечно дело с остальной частью сущности продукта в AngularJs способом.