[Solved] Child document properties lost when inserting via a method

I’m trying to send a order with a child order line to my insert method, but the child order line is being inserted as empty object.

The data I’m sending looks like this:

{
  "orderLines":
  [ {"description":"d1","quantity":0,"unitPrice":0,"lineValue":0,"isNewLine":true,"createdAt":"2016-02-12T10:47:02.240Z"}],
  "createdAt":"2016-02-12T10:47:02.222Z",
  "deliveryAddress1":"o1"
}

But the mongo record ends up looking like this - there is an order line in the array, but it is an empty record:

{
  "_id": "s9xvSnH2HemvKZ287",
  "orderLines": [
    {}
  ],
  "createdAt": "2016-02-12T10:47:02.222Z",
  "deliveryAddress1": "o1"
}

I’m calling the method like this:

// call the method for upserting the data
Orders.methods.insert.call({
    orderId: orderId,
    data: order
}, (err, res) => {
    if (err) {
        sAlert.error(err.message);
    } else {
        sAlert.success("Save successful");
        FlowRouter.go("/");
    }
});

The ValidatedMethod looks like this:

Orders.methods.insert= new ValidatedMethod({

    name: 'Orders.methods.insert',

    validate(args) {
        var schemaContext = Schemas.OrderSchema.namedContext("OrderForm");
        schemaContext.validate(args.data);
    },

    run(args)
    {
        console.log("order", JSON.stringify(args.data));

        return Orders.insert(args.data);
    }
});

And finally the schema looks like this:

Schemas.OrderSchema = new SimpleSchema({
    notes: {
        type: String,
        optional: true
    },
    deliveryAddress1: {
        type: String,
        optional: true
    },
    orderLines: {
        type: [Object],
        optional: true
    },
    createdAt: {
        type: Date,
        optional: false,
        label: "Date created"
    }
});

Is there anything obviously wrong? I’ve been banging my head against this for hours!

The objects you’re submitting in your orderLines array aren’t specified in the schema, so by default Simple Schema is cleaning (stripping) them. Take a look at the Simple Schema docs to see how to define an array in your schema while at the same time specifying the potential object properties that will be in that array. For example:

MySchema = new SimpleSchema({
    addresses: {
        type: [Object],
        minCount: 1,
        maxCount: 4
    },
    "addresses.$.street": {
        type: String
    },
    "addresses.$.city": {
        type: String
    }
});
1 Like

Ahhhh… genius, thank you - that was driving me nuts.

I’d originally tried to specify a child schema as follows, which hadn’t worked either, and then changed to the code in my question:

Schemas.OrderSchema = new SimpleSchema({
    orderLines: {
        type: [OrderLineSchema],
    },
});

I had another look through the code in the simple schema docs and it seemed like the above should work and then the true cause of my original problem finally clicked; I was declaring the collection schema before the child schema. I’m still thinking in C# properties/methods rather than JavaScript.