The server has a Meteor Method
that returns a GiftList
object that contains a Gift
set.
The client has a Meteor Call
that prints out the result. The Gift
set is undefined even though it is initialised and sent by the server. The instance variables don't seem to be included in the response even though the server has sent it.
Gift List
import {Gift} from "../gift/Gift";
export class GiftList {
private _id: number;
private _personName:string;
private _user: User;
private _gifts: Set<Gift>;
get id(): number {
return this._id;
}
set id(value: number) {
this._id = value;
}
get personName(): string {
return this._personName;
}
set personName(value: string) {
this._personName = value;
}
get user(): User {
return this._user;
}
set user(value: User) {
this._user = value;
}
get gifts(): Set<Gift> {
return this._gifts;
}
set gifts(value: Set<Gift>) {
this._gifts = value;
}
}
Gift
import {GiftList} from "../giftlist/GiftList";
export class Gift {
private _id: number;
private _name: string;
private _description: string;
private _isPrivate: boolean;
private _cost: number;
private _giftList: GiftList;
get id(): number {
return this._id;
}
set id(value: number) {
this._id = value;
}
get name(): string {
return this._name;
}
set name(value: string) {
this._name = value;
}
get description(): string {
return this._description;
}
set description(value: string) {
this._description = value;
}
get isPrivate(): boolean {
return this._isPrivate;
}
set isPrivate(value: boolean) {
this._isPrivate = value;
}
get cost(): number {
return this._cost;
}
set cost(value: number) {
this._cost = value;
}
get giftList(): GiftList {
return this._giftList;
}
set giftList(value: GiftList) {
this._giftList = value;
}
}
Server - Meteor Method
Meteor.methods({
"getGiftLists": function (): GiftList[] {
const giftList: GiftList = new GiftList();
giftList.gifts = new Set();
const gift: Gift = new Gift();
gift.name= "Example gift";
gift.description = "Description of gift";
giftList.gifts.add(gift);
// I've printed the value here and the gift list definitely contains gifts as expected.
return [giftList]
}
})
Client - Meteor Call
Meteor.call("getGiftLists", {}, (err: any, res: GiftList[]) => {
if (err) {
alert(err);
} else {
console.dir(res); // Defined
console.log(res.length) // 1
console.dir(res[0].gifts); // Undefined
callback(res);
}
});
Question
Why is the Gift
set undefined?
I believe the problem here is that Metoer's EJSON doesn't know how to serialize a Set
to be sent to the client. EJSON provides a way to define new types and how they should be serialized and de-serialized. Have a look at the EJSON docs.