[Solved] Transfer object with array via Meteor.call

Hey,
I’m having in issue when I transfer a more complex object via Meteor.call.

Meteor.call("addData",{name,desc,items});

while items is an array like this:

let items = [];
items['a'] = "A name";
items['b'] = "Just another text";

On the server side I only get an empty array:

Meteor.methods("addData",function(obj) {
console.log(obj);
// {name:'Test',desc:'Just a description', items:[]}

});

Is this not possible with methods?

Okay, just saw that JSON doesn’t allow arrays with named keys.

An array with named keys is an object:

bob['height'] = 1.8;
bob['hair'] = 'blonde'; // is the same as

bob = {};
bob.height = 1.8;
bob.hair = 'blonde'; // is the same as

bob = {
  height: 1.8,
  hair: 'blonde'
}

Sure, but if you do the following

let bob = [];
bob['height'] = 1.8;
bob['hair'] = 'blonde';

JSON.stringify(bob);

You will get [] as response. That was the case as I’ve transferred my “array” via Meteor.call. I’ve read on SO that named keys aren’t allowed within an “array”, instead you have to use a “real” object.

Yes - that was my point :slight_smile:

1 Like