【问题标题】:How to insert array into object?如何将数组插入对象?
【发布时间】:2015-12-23 02:58:49
【问题描述】:

我正在使用 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" ,
        }
    ]
}

【问题讨论】:

  • 您的查询应该搜索是否存在产品 User.findOne("product").exec(function (err, product) { product.key.push({ /* 不管 / }) ; product.save(function (err) { / all done */ }); }); stackoverflow.com/questions/18161056/…

标签: angularjs mongodb sails.js


【解决方案1】:

根据我对 AngularJS 的个人经验,你为什么要引用 使用 product.attributes 输入 ng-model?

你试过了吗:

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

像你一样:

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

当然还要以 AngularJs 的方式处理产品实体的其余部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2020-03-30
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多