1

服务器有一个 Meteor Method,它返回一个GiftList包含Gift集合的对象。

Call客户端有一个打印结果的 Meteor 。该Gift集合是未定义的,即使它是由服务器初始化和发送的。即使服务器已经发送了实例变量,它似乎也没有包含在响应中。

礼品清单

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;
    }
}

礼物

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;
    }
}

服务器 - Meteor 方法

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]
    }
})

客户端 - 流星呼叫

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);
    }
});

问题

为什么Gift集合未定义?

4

1 回答 1

2

我相信这里的问题是 Metoer 的 EJSON 不知道如何序列化 aSet以发送到客户端。EJSON 提供了一种定义新类型以及它们应该如何序列化和反序列化的方法。查看 EJSON 文档。

https://docs.meteor.com/api/ejson.html

于 2021-02-20T22:57:44.077 回答