You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular-Nativescript数组首元素出现异常Zone对象求助

NativeScript Angular中http请求返回数组混入Zone Promise对象的问题

我最近用tns create myApp --ng新建了一个TNS项目,按照文档要求引入了httpModule,原本应该能正常从远程服务器的JSON接口获取数据,但现在碰到了个问题:返回的数组第一个元素是个异常对象,直接导致后续操作报错。

这个异常对象的dump信息如下:

JS: ==== object dump start ==== 
JS: 0: { 
JS: "__zone_symbol__state": null, 
JS: "__zone_symbol__value": [] 
JS: } 
JS: 1: { 
JS: "id": 1, 
...

数组里后续的元素都是正常的数据对象。

我的服务代码(item.service.ts)是这样的:

import { Injectable } from "@angular/core";
import { Item } from "./item";

const httpModule = require("http");

@Injectable()
export class ItemService {
    private items = new Array<Item>(
        httpModule.request({
            url: "https://www.example.com/data.json",
            method: "GET"
        }).then((response) => {
            var obj = response.content.toJSON();
            for (let i=0; i<obj.length; i++) {
                //console.log(obj[i]);
                this.items.push(new Item(obj[i].id, obj[i].name, obj[i].role, obj[i].vereine, obj[i].testAufgaben));
                //console.log("items object testaufgaben: "+this.items[i].testAufgaben)
                console.dir(this.items);
            }
            //console.log(obj);
        }, (e) => {
            console.log(e);
        })
    );

    getItems(): Item[] {
        return this.items;
    }

    getItem(id: number): Item {
        return this.items.filter(item => item.id === id)[0];
    }
}

我的模型代码(item.ts)如下:

export class Slot {
    constructor(
        public slot: string,
        public fillIn: boolean
    ) {}
}

export class Aufgabe {
    constructor(
        public aufgabe: string,
        public aufgabeSlots: Array<Slot>
    ) {}
}

export class Item {
    constructor(
        public id: number,
        public name: string,
        public role: string,
        public vereine: Array<string>,
        public testAufgaben: Array<Aufgabe>
    ) {}
}

想请教下大家:这个异常对象到底是什么?我该怎么避免这个问题?感谢各位的解答!

内容的提问来源于stack exchange,提问作者stefanvz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:50:37