下面的问题已经花了我6个小时的时间,所以我真的希望你能帮助我。
我现在的目标是通过针对Nativescript应用程序的StrongLoop API从mongodb检索对象列表(自定义类型/“幻想”)。我的app.component.ts中的代码如下所示(函数getFantasies()
中出现错误:
import { Component, OnInit } from "@angular/core";
import { Fantasy } from "../../sdk/models/Fantasy";
import { LoopBackConfig, FantasyApi } from "../../sdk";
@Component({
moduleId: module.id,
selector: "Itemlist",
templateUrl: "./itemlist.component.html",
styleUrls: ["./itemlist-common.css", "./itemlist.css"],
providers: [FantasyApi]
})
export class ItemlistComponent implements OnInit {
private fantasies: Array<Fantasy> = new Array();
constructor(
private fs: FantasyApi) {
LoopBackConfig.setBaseURL('http://localhost:3000');
LoopBackConfig.setApiVersion('api');
}
ngOnInit() {
this.getFantasies();
}
getFantasies(): void {
this.fs.find()
.subscribe(res => this.fantasies = res)
}
错误来自最后一行的“this.fantasies”并说:
[ts]
Type '{}[]' is not assignable to type 'Fantasy[]'.
Type '{}' is not assignable to type 'Fantasy'.
[ts]
Type '{}[]' is not assignable to type 'Fantasy[]'.
Type '{}' is not assignable to type 'Fantasy'.
Property '"category"' is missing in type '{}'.
模型“幻想”有一些非可选属性,“类别”是第一个。
根据我的理解,该函数的默认空返回对象{}
会导致“幻想”模型的强制属性(在本例中不存在)出现问题。
我怎么能解决这个问题?
提前谢谢!