0

I'm building app using SailsJS, AngularJS and MongoDB, I'm confused how to insert data array into object. This is example my schema

Asset.js

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

This is my view form

<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>

This is the my query

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

What is the problem in my query,no error but value in key only array no object.

I want result to be this

{ 
    "_id" : 5,
    "product" : 3,
    "store" : 2
    "key": [
        { 
            "attribute": "xxxxxxx",
            "name": "book",
            "additionaPrice": "xxxx",
            "file": "xxxx" ,
        }
    ]
}
1
  • 1
    your query should search if exist a product User.findOne("product").exec(function (err, product) { product.key.push({ /* whatever / }); product.save(function (err) { / all done */ }); }); stackoverflow.com/questions/18161056/… Commented Dec 24, 2015 at 16:35

1 Answer 1

0

From my personal experience with AngularJS, why are you referencing inputs ng-model with product.attributes?

Have you tried this :

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

like you did :

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

And of course deal with rest of product entity in AngularJs way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.