Meteor.call losing json data?


#1

Never ran in to this problem before, and it MAY have something to do with ViewModel (it seems to work properly without VM?)

Basically I have a function that is sending some product data to the server. VM version is not working but the data is appearing in the object before sending. Non-VM version seems to work properly.

The data that is being lost is an array of strings.

From VM Version:

submitKit: function() {
        var kitProduct = {
            shipBox: this.kitContentsShipbox(),
            partCategory: this.kitSelectedPartCat(),
            weight: this.kitContentsWeight(),
            kitPartTypes: this.kitPartTypes(),
            kitPartCodes: this.kitPartCodes()
        };
        console.log(' length : ' + kitProduct.kitPartTypes.length);
        console.log(' length : ' + kitProduct.kitPartCodes.length);
        Meteor.call('kitInsert', kitProduct, function (error, result) {
            if (!handleErrors(error, result)) {
                FlowRouter.go('/products/product/' + result);
            }
        });
    },

This code results in Console:

length : 2
length : 2

Now to see the beginning of the kitInsert method on server:

kitInsert: function (attributes) {
        check(Meteor.userId(), String);
        console.log('.kitPartTypes.length: ' + attributes.kitPartTypes.length);
        console.log('.kitPartCodes.length: ' + attributes.kitPartCodes.length);

This results in:

I20160525-08:50:01.888(-7)? .kitPartTypes.length: undefined
I20160525-08:50:01.889(-7)? .kitPartCodes.length: undefined

Yet if I try calling this function WITHOUT VM:

        var kitProduct = {
            shipBox: $(e.target).find('[name=shippingBox]').val(),
            partCategory: $(e.target).find('[name=partCategory]').val(),
            kitPartTypes: kitPartTypes,
            kitPartCodes: kitPartCodes,
            weight: $(e.target).find('[name=addWeight]').val()
        };

        // validate
        var errors = validateKit(kitProduct);
        if (errors.shipBox || errors.partCategory || errors.kitPartTypes || errors.weight)
            return Session.set('productSubmitErrors', errors);

        console.log(' length : ' + kitProduct.kitPartTypes.length);
        console.log(' length : ' + kitProduct.kitPartCodes.length);
        
        // call the insert function
        Meteor.call('kitInsert', kitProduct, function (error, result) {

            // display the error to the user and abort
            if (!handleErrors(error, result)) {
                FlowRouter.go('/products/product/' + result);
            }
        });

Results in:

length : 2
length : 2

And it adds properly?

It looks to me as the JSON is equivalent in both examples. But for some reason the VM example is losing the data on send?

Any ideas? I’m not even sure what to attempt to debug from here?

(Edit: Additional Info -

I attempted logging the attributes object on the server. With VM the results:

I20160525-08:58:22.447(-7)? { shipBox: 'Medium Box',
I20160525-08:58:22.448(-7)?   partCategory: 'Mount',
I20160525-08:58:22.448(-7)?   weight: '10',
I20160525-08:58:22.448(-7)?   kitPartTypes: { '0': 'EMT', '1': 'EMT' },
I20160525-08:58:22.448(-7)?   kitPartCodes: { '0': '0007', '1': '0007' } }

Without VM:

I20160525-09:00:20.440(-7)? { shipBox: 'Medium Box',
I20160525-09:00:20.440(-7)?   partCategory: 'Mount',
I20160525-09:00:20.441(-7)?   kitPartTypes: [ 'EMT', 'EMT' ],
I20160525-09:00:20.441(-7)?   kitPartCodes: [ '0007', '0007' ],
I20160525-09:00:20.441(-7)?   weight: '10' }

So it seems VM is turning the array in to a Reactive Array?

To resolve this, could simply turn the reactive array in to a regular one on creation of the json:

        kitPartTypes: this.kitPartTypes().array(),
        kitPartCodes: this.kitPartCodes().array()

I guess I answered my own question, but I’ll leave this up incase it might help someone in the future!