3

when i want to insert a document in my mongodb with monk wich has an array element of subdocuments, the funcion insert it wrong in the db. I'm calling the insert function this way:

var OrderItem = [];
OrderItem[0] = {
                'model': asd1,
                'size' : M,
                'color' : Black
            };

OrderItem[1] = {
                'model': bsa2,
                'size' : S,
                'color' : Black
            };
var newOrdenCompra = {
            'fechaCompra' : d,
            'items' : OrderItem
        };
 $.ajax({
        type: 'POST',
        data: newOrdenCompra,
        url: '/order/addordercompra',
        dataType: 'JSON'
        }).done(function( response ) 
        {
            if (response.msg !== '') {
                alert('Error: ' + response.msg);
            }
        });

and then:

/*
 * POST to add orden de compra.
 */
router.post('/addordercompra', function(req, res) {
    var db = req.db;
    var collection = db.get('ordercompra');
    collection.insert(req.body, function(err, result){
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});

But what i get into the BD is something like this (the example that i wrote above have less atributes in the item subdocuments):

> db.ordercompra.find().pretty()
{
        "_id" : ObjectId("5601b2181430470c1266d415"),
        "fechaCompra" : "2015-09-22T16:54:59Z",
        "items[0][model]" : "CLUB DE LA PELEA",
        "items[0][size]" : "XXXS",
        "items[0][color]" : "CHOCOLATE",
        "items[0][sena]" : "1200",
        "items[0][precio]" : "2600",
        "items[0][ordOT]" : "2",
        "items[0][id]" : "55f9e402ebfcd9b414339f8f",
        "items[1][model]" : "302",
        "items[1][size]" : "M",
        "items[1][color]" : "NEGRO",
        "items[1][sena]" : "0",
        "items[1][precio]" : "2100",
        "items[1][ordOT]" : "",
        "items[1][id]" : "55e76c0d497c742019bbb5f3"
}
>

What can i do to get the structure of an element with an array of subdocuments? am i doing it wrong or is the insert what's failing?

Thanks for the help! and sorry for my bad english. Nicolas.

4
  • 1
    You should probably JSON.stringify(data) on client and then JSON.parse(req.body) on server Commented Sep 22, 2015 at 21:00
  • Hi!, thanks for help!, i can send the JSON.stringifu(newOrdenCompra) to the server, but when i test it, if i parse the req.body, then the function crashes. And if i don't parse it, the function just save a string with all the data. Do you have any idea ? Commented Sep 22, 2015 at 22:35
  • Also your $.ajax dataType should be "application/json; charset=utf-8" rather then JSON what do you mean by "the function crashes". Anyway I think you should insert only fechaCompra without items and than result.items =[] and in a loop result.items.push(items[i]) and than result.save() Commented Sep 23, 2015 at 14:26
  • Thanks for the help Molda!, but i solved the problem changing the dataType to 'text' and adding contentType as 'application/json', doing that i didn't need to parse the req.body, i'd just send req.body to the insert function and it worked. – And with Crashes i mean that the parse function throws an 'Unexpected token o' error, beacause the object req.body is already an object. Commented Sep 23, 2015 at 15:32

1 Answer 1

2

I've solved the problem sending the data transformed with JSON.stringify and changed the ajax parameters to dataType : 'text' and adding contentType: 'application/json'. Doing that the insert works perfect. Thanks for the help! Example Imgs:

client code

server code

And the BD looks like this:

bd data

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.