找到正确的值时如何停止响应错误

时间:2019-02-07 15:59:46

标签: typescript response nativescript

我有这个功能:

finddevice(s: any) {
    if (s !== '') {
        for (let i = 1; i < 255; i++) {
            this.ws.ping(s + '.' + i).subscribe(res => {
                console.log('res', res)
                if (res) {
                    try { throw i }
                    catch (ii) {
                        let myipfind = s + '.' + ii;
                        this.myipdevice = myipfind;

                    }
                }
            },(error) => {
                console.log('got error from finddevice', error);
            })
        }
    }
}

在255个IP中,只有一个IP获得正确的值。此JSON:

{
        "value1": "1",
        "value2": "0",
}

和另一个IP出现错误,如下所示:

JS: {
JS:   "headers": {
JS:     "normalizedNames": {},
JS:     "lazyUpdate": null,
JS:     "headers": {}
JS:   },
JS:   "status": 0,
JS:   "statusText": "Unknown Error",
JS:   "url": null,
JS:   "ok": false,
JS:   "name": "HttpErrorResponse",
JS:   "message": "Http failure response for (unknown url): 0 Unknown Error",
JS:   "error": {
JS:     "originalStack": "Error: java.net.NoRouteToHostException: No route to host\n    at new c (file:///data/data/org.nativescript.project/files/app/vendor.js:1:1217836)\n    at file:///data/data/org.nativescript.project/files/app/vendor.js:1:1083876\n    at Object.onComplete (file:///data/data/org.nativescript.project/files/app/vendor.js:1:1084998)",
JS:     "zoneAwareStack": "Error: java.net.NoRouteToHostException: No route to host\n    at new c (file:///data/data/org.nativescript.project/files/app/vendor.js:1:1217836)\n    at file:///data/data/org.nativescript.project/files/app/vendor.js:1:1083876\n    at Object.onComplete (file:///data/data/org.nativescript.project/files/app/vendor.js:1:1084998)"
JS:   }

我想在发现正确值时停止响应。知道如何停止响应吗?

0 个答案:

没有答案