Angular5 - 错误:使用无效数据调用函数DocumentReference.set()。数据必须是一个对象,但它是:一个自定义的PlatformModel对象

时间:2018-04-30 18:34:30

标签: javascript typescript firebase angular5 angularfire2

我发现google上已有一些帖子可供人们解决此问题。但我无法重现我项目的解决方案。

我的界面:

declare module PlatformInterface {

    export interface Design {
        primaryColor: string;
        backgroundImage: string;
    }

    export interface Saga {
        id: string;
        name: string;
        short_desc: string;
        desc: string;
        manga: Manga[];
        anime: Anime[];
    }

    export interface Root {
        id: string;
        name: string;
        design: Design[];
        saga: Saga[];
    }

}

我的模特:

export class PlatformModel implements PlatformInterface.Root {
    id: string;
    name: string;
    design = [];
    saga = [];

    constructor(obj?: any) {
        this.id = obj.name.toLowerCase().replace(' ', '-');
        this.name = obj.name;
        this.design = obj.design;
        this.saga = obj.saga;
    }
}

我的服务:

@Injectable()
export class PlatformService {

    public list$: Observable<PlatformModel[]>;

    private _platform: AngularFirestoreCollection<PlatformModel>;

    constructor(db: AngularFirestore) {
        this._platform = db.collection<PlatformModel>('platforms');
        this.list$ = this._platform.valueChanges();
    }

    /** Get Platform by id */
    get(id: string): Observable<PlatformModel> {
        return this._platform.doc<PlatformModel>(id).valueChanges();
    }

    /** Add / Update Platform */
    set(id: string, platforms: PlatformModel) {
        return fromPromise(this._platform.doc(id).set(platforms));
    }

    /** Remove Platform */
    remove(id: string) {
        return fromPromise(this._platform.doc(id).delete());
    }

}

我在Component.ts中的功能

constructor(public _platformService: PlatformService) {
}

addPlatform(name: string) {
    if (name !== '') {
        const platform = new PlatformModel({
            name: name,
            design: [],
            saga: []
        });

        this._platformService.set(platform.id, platform).subscribe();
    }
}

Angular编译器不会抛出任何错误,但是当我尝试触发addPlatform函数时,我在浏览器中遇到此错误:

ERROR Error: Function DocumentReference.set() called with invalid data. Data must be an object, but it was: a custom PlatformModel object

错误说数据必须是一个对象,但它已经是一个对象了?我的意思是我在服务中定义它:

public list$: Observable<PlatformModel[]>;

[]使它成为某个对象?

2 个答案:

答案 0 :(得分:2)

我在这里找到了一些澄清Firestore: Add Custom Object to db

  

虽然firebase可以将对象内部的数据发送到数据库,但是当数据返回时,它无法将其实例化回到类的实例中。因此不允许上课

我的自定义类的解决方法是

 this.db.collection(`${this.basePath}/`).doc(custom_class.$key)
.set(Object.assign({}, JSON.parse(JSON.stringify(custom_class))))
.then( ret => {
  log.debug('file added', ret);
}).catch( err => {
  log.error(err);
});

所以我想你的情况就是

    /** Add / Update Platform */
set(id: string, platforms: PlatformModel) {
    return fromPromise(this._platform.doc(id).set(Object.assign({},JSON.parse(JSON.stringify(platforms))));
}

答案 1 :(得分:0)

要将地图添加到Firestore文档中,您将必须使用:

Object.assign({},JSON.parse(JSON.stringify(YOUR_MAP)))