Search code examples
javascripttypescriptmeteor

Meteor Call object is different to the received Meteor Method object


I'm sending an object from the client to the server using a Meteor Call and Meteor method.

When the object is received in the Method it looks different. It is nested within the giftList

Meteor Call - JSON.stringify

{"personName":"Default person name","username":"","gifts":[]}

Meteor Method

{"giftList":{"personName":"Default person name","username":"","gifts":[]}}

Meteor Call Code

console.log(JSON.stringify(giftList))  // {"personName":"Default person name","username":"","gifts":[]}

Meteor.call("addGiftList", {giftList}, (err: any, res) => {});

Meteor Method code

Meteor.methods({
    "addGiftList": function (giftList: GiftList): void {
        console.log(JSON.stringify(giftList))  // {"giftList":{"personName":"Default person name","username":"","gifts":[]}}
        return GiftListCollectionManager.getInstance().insert(giftList);
    }
});

GiftList

export class GiftList {

    personName: string = "";

    username: string = "";

    gifts: Gift[] = [];
}

Why is the received object different and what is the correct method of handling this?


Solution

  • I think the reason is your way of passing giftList to Meteor.call. Passing in {giftList} is shorthand for { giftList: giftList }.

    Try the following:

    Meteor.call("addGiftList", giftList, (err: any, res) => {});