简体   繁体   中英

Ionic 3. Typescript Map object not being saved to Firebase

I am using the Ionic 3 framework.

I am trying to save an object to Firebase, but not all the properties are being saved.

The string properties are saved, but the Map and custom objects (Event) are not being saved.

Here are the objects with the properties I'm trying to save:

=============================================================

export class Room extends WorldObject {
private entityList: Array<Entity>;
private itemList: Array<Item>;
private directionMap: Map<string, number>;
private onEnterEvent: Event;
private onExitEvent: Event;
private actionEventMap: Map<string, Map<string, Event>>;

=============================================================

=============================================================

export class Event {

private actionScript: string;
private message: string;

=============================================================

So, again, the string and Array properties are saved, but the properties of type "Map" or "Event" are not being saved to firebase.

What could be the issue?

Does Firebase not like custom typescript objects?

Am I supposed to convert them somehow before saving? I shouldn't have to.

Any help is appreciated

the object should provide a toString() function to serialize the object to string. It seems that he Map in typescript has no such method.

So I found a workaround. I hate workarounds! Because I'm a big fan of best practices. So, here goes...

Instead of using the Typescript Map type, since it's not yet supported in Firebase, I've created a map object using another way, referenced here:

How do I dynamically assign properties to an object in TypeScript?

export class Room extends WorldObject {

private entityList: Array<Entity>;
private itemList: Array<Item>;
private directionMap: {[key:string]: number};
private onEnterEvent: Event;
private onExitEvent: Event;
private actionEventMap: {[key:string]: {[key:string]: Event}};

Instead of using

let newRoom = Map<string, number>

newRoom.set('north', 1);

I used:

let newRoom: {[key:string]: number} = {}
newRoom['north'] = 1;

I should probably encapsulate this in a class. Since best practices are awesome.

And as a result, I was able to persist data to firebase.

That is all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM