Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Meteor调用结果具有未定义的实例变量_Javascript_Typescript_Meteor_Meteor Methods - Fatal编程技术网

Javascript Meteor调用结果具有未定义的实例变量

Javascript Meteor调用结果具有未定义的实例变量,javascript,typescript,meteor,meteor-methods,Javascript,Typescript,Meteor,Meteor Methods,服务器有一个Meteor方法,该方法返回一个礼物列表对象,该对象包含一个礼物集 客户端有一个Meteor调用,用于打印结果。礼物集未定义,即使它是由服务器初始化和发送的。即使服务器已发送实例变量,响应中似乎也不包含实例变量 礼品清单 import {Gift} from "../gift/Gift"; export class GiftList { private _id: number; private _personName:string; p

服务器有一个Meteor
方法
,该方法返回一个
礼物列表
对象,该对象包含一个
礼物集

客户端有一个Meteor
调用
,用于打印结果。礼物集未定义,即使它是由服务器初始化和发送的。即使服务器已发送实例变量,响应中似乎也不包含实例变量

礼品清单

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.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);
    }
});
问题


为什么礼物集未定义?

我认为这里的问题是,Metor的EJSON不知道如何序列化要发送给客户端的
集。EJSON提供了一种定义新类型以及如何序列化和反序列化它们的方法。看看EJSON文档


那就更有意义了!明天我将尝试使用数组。在这种情况下是否应该抛出错误?我想说,它可能会抛出错误。事实并非如此,这可能是因为在编写此代码时,映射和集不是ECMAScript规范的一部分。可能是一个很好的bug报告/功能请求。