Javascript筛选器数组抛出错误并崩溃
Javascript filter array throwing errors and crashing
我正在编写一个基于NativeScript的移动应用程序,使用Typescript/JavaScript。此代码片段是在蓝牙扫描完成后调用的。我需要找到正确的服务。我的代码一直有效,直到我尝试从选项中选择我想要的服务。services.filter(function (obj) {
行似乎导致应用程序崩溃,但错误日志对我来说没有多大意义…
console.log("Connect Variable");
var services: Array<string>;
var service: any;
services = []; //initialise array
bluetooth.connect(
{
UUID: _peripheral.UUID,
// NOTE: we could just use the promise as this cb is only invoked once
onConnected: function (peripheral) {
console.log("------- Peripheral connected: " + JSON.stringify(peripheral));
// Put all Services into an Array
peripheral.services.forEach(function (value) {
console.log("---- ###### adding service: " + value.UUID);
services.push(value);
});
//search for the correct service
service = peripheral.services.filter(function (obj) { // <- PROBLEM LINE ****************** Caudsing the app to crash out ...
return obj.UUID == 'A000';
});
},
}
);
这是控制台日志:
CONSOLE LOG file:///app/Pages/Home/home.component.js:145:32: ---- ###### adding service: A000
CONSOLE LOG file:///app/Pages/Home/home.component.js:145:32: ---- ###### adding service: 180A
***** Fatal JavaScript exception - application has been terminated. *****
Native stack trace:
1 0xbea99 NativeScript::FFICallback<NativeScript::ObjCMethodCallback>::ffiClosureCallback(ffi_cif*, void*, void**, void*)
2 0x4ae381 ffi_closure_inner_SYSV
3 0x4b20b8 ffi_closure_SYSV
4 0x25e59d15 <redacted>
5 0x25e59e4b <redacted>
6 0x25e5f9f3 <redacted>
7 0x208d1823 <redacted>
8 0x208d180f <redacted>
9 0x208dfba9 <redacted>
10 0x20d25bdd <redacted>
11 0x20d240d7 <redacted>
12 0x20c732e9 CFRunLoopRunSpecific
13 0x20c730d5 CFRunLoopRunInMode
14 0x22263ac9 GSEventRunModal
15 0x253380b9 UIApplicationMain
16 0x4b202c ffi_call_SYSV
17 0x4ae0c3 ffi_call
18 0x91a0b NativeScript::FFICall::call(JSC::ExecState*)
19 0x2e4931 JSC::LLInt::setUpCall(JSC::ExecState*, JSC::Instruction*, JSC::CodeSpecializationKind, JSC::JSValue, JSC::LLIntCallLinkInfo*)
20 0x2e2579 llint_slow_path_call
21 0x2ea1ed llint_entry
22 0x2ea1f9 llint_entry
23 0x2ea1f9 llint_entry
24 0x2ea4c1 llint_entry
25 0x2ea1f9 llint_entry
26 0x2e5021 vmEntryToJavaScript
27 0x2a4cd9 JSC::JITCode::execute(JSC::VM*, JSC::ProtoCallFrame*)
28 0x28d713 JSC::Interpreter::executeCall(JSC::ExecState*, JSC::JSObject*, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
29 0x38be87 JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&, WTF::NakedPtr<JSC::Exception>&)
30 0x9e943 NativeScript::GlobalObject::moduleLoaderEvaluate(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSValue, JSC::JSValue)
31 0x44c4f9 JSC::ModuleLoaderObject::evaluate(JSC::ExecState*, JSC::JSValue, JSC::JSValue)
JavaScript stack trace:
1 onConnected@file:///app/Pages/Home/home.component.js:157:55
2 peripheralDidDiscoverCharacteristicsForServiceError@file:///app/tns_modules/nativescript-bluetooth/bluetooth.js:111:23
3 UIApplicationMain@[native code]
4 start@file:///app/tns_modules/application/application.js:233:26
5 @file:///app/tns_modules/nativescript-angular/application.js:65:26
6 ZoneAwarePromise@file:///app/tns_modules/zone.js/dist/zone-node.js:542:38
7 nativeScriptBootstrap@file:///app/tns_modules/nativescript-angular/application.js:64:23
8 anonymous@file:///app/main.js:5:36
9 evaluate@[native code]
10 moduleEvaluation@[native code]
11 @[native code]
12 promiseReactionJob@[native code]
JavaScript error:
我做错了什么?
更新:
根据Vladimir的回答,我得到了这个:
for (let i = 0; i < peripheral.service.count; i++) {
if (peripheral.services.objectAtIndex(i).UUID == 'A000') {
service = peripheral.services.objectAtIndex(i);
console.log("selected service: ");
}
}
然而,我仍然得到这个输出:
JavaScript错误:file:///app/Pages/Home/home.component.js:98:56:JS ERROR TypeError:undefined不是对象(正在评估"peripheral.service.count")
很可能这是NSArray而不是JavaScript数组,这就是为什么不能使用filter()
的原因。可能的解决方案是循环本机NSArray
并只复制所需的对象,或者您可以复制所有对象并稍后使用filter
。以下是如何复制所有对象的示例:
var services = [];
for (let i = 0; i < peripheral.services.count; i++) {
services.push(peripheral.services.objectAtIndex(i));
}
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Javascript筛选器数组抛出错误并崩溃
- MEAN CRUD错误崩溃无法设置标头
- SASS错误导致的奇怪的吞咽崩溃
- 主干/下划线 js “太多递归”和“检查目标崩溃”错误
- 节点.js - 事件.js:154 抛出错误写入 EPIPE;程序崩溃
- can'的中间崩溃返回错误;t设置标题
- Node.Js服务器不断崩溃,没有任何错误
- Bluemix在下一个错误时崩溃
- 加载错误的页面时,iMacro脚本崩溃
- 为什么我得到“;您的应用程序正在崩溃错误“;在Meteor.js教程中
- 代码中的错误导致浏览器选项卡崩溃
- Javascript崩溃HTML -我的代码不工作,你能看到任何错误吗?
- amqplib, checkQueue()崩溃我的应用程序时,错误命中
- 引导崩溃错误的行为
- 麻烦的错误,未能拒绝与蓝鸟.解决得很好.错误似乎会使应用程序崩溃
- 嵌套的JSON.解析错误和JS对象遍历错误没有被JS的try/catch捕获,崩溃服务器
- 崩溃后从Safari记录JavaScript错误
- 防止节点类型错误使应用程序崩溃
- JQuery/Javascript - Firefox在简单任务上崩溃,没有控制台错误