javascript - Meteor 调用结果有未定义的实例变量
问题描述
服务器有一个 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
集合未定义?
解决方案
我相信这里的问题是 Metoer 的 EJSON 不知道如何序列化 aSet
以发送到客户端。EJSON 提供了一种定义新类型以及它们应该如何序列化和反序列化的方法。查看 EJSON 文档。
推荐阅读
- ios - 如何避免将 REVERSED_CLIENT_ID 硬编码到 iOS Info.plist 中以进行开发/产品 Google 登录?
- android - Firestore:连接时是否可以不执行侦听器?
- shell - 两个参数的简单shell程序(并发问题)
- typescript - 如何在 Dexie 的 WhereClause.inAnyRange(ranges, options) 中提供参数选项?
- javascript - React Redux 忽略了调度
- rest - 对资源的 Http 幂等部分修改最好实现为 PUT 而不是 PATCH?
- postgresql - 缓存 PostgreSQL 表 - 客户端连接过多
- php - 为什么我的 php 代码不打印提交的表单?
- java - 数据库错误 - 无法从“事务”表中选择数据:没有这样的列:“日期” - Java 错误
- ethereum - 我可以避免在以solidity 编写的用于gas 优化的以太坊智能合约中进行验证吗?