2016-04-16 8 views
2

次のコードを使用して、見つかったすべてのデバイスに対してアレイにデバイスを追加するBluetoothデバイスをスキャンします。Typescriptの配列に "push()"を入力します

devices: Observable<Array<string>>; 

bluetoothAdd() { 
    this.isScanning = true; 
    var plusIcon = this.page.getViewById("add"); 
    plusIcon.style.opacity = 0; 

    var self = this; 
    bluetooth.hasCoarseLocationPermission().then(
     function (granted) { 
      if (!granted) { 
       bluetooth.requestCoarseLocationPermission(); 
      } else { 
       bluetooth.startScanning({ 
        serviceUUIDs: ["133d"], 
        seconds: 4, 
        onDiscovered: function (peripheral) { 
         console.log("Periperhal found with UUID: " + peripheral.UUID); 
         this.devices.push(peripheral); // <- Problem Line 
        } 
       }).then(function() { 
        console.log("scanning complete"); 
        self.isScanning = false; 
        plusIcon.style.opacity = 1; 
       }, function (err) { 
        console.log("error while scanning: " + err); 
       }); 
       this.isScanning = false; 
      } 
     }); 
} 

しかし、このコードは、次のエラーがスローされます。

JavaScript error: file:///app/Pages/Home/home.component.js:99:37: JS ERROR TypeError: undefined is not an object (evaluating 'this.devices.push')

私は活字体で働いていますが、私はプッシュ機能は、JSの事を知っています。私がTypescriptでこれをどうやって行うのか分かりません - どうしたのですか?

+1

これはTypeScriptとは関係ありません。 「これはあなたが期待するものではありません。 http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-workを参照してください。あなたは 'self'を使うことができます。 – elclanrs

答えて

3

これはTypeScriptとは関係ありません。それはの普通のJavascriptの規則です。です。

問題あなたの代わりにクラスのonDiscoveredに与える機能をこのポイント。

あなたが定義した自己変数を使用するかのように、代わりに矢印の機能を使用するようにコードを書き換えることにより、それを修正することができます:

devices: Observable<Array<string>>; 

bluetoothAdd() { 
    this.isScanning = true; 
    var plusIcon = this.page.getViewById("add"); 
    plusIcon.style.opacity = 0; 


    bluetooth.hasCoarseLocationPermission().then(
     (granted) => { 
      if (!granted) { 
       bluetooth.requestCoarseLocationPermission(); 
      } else { 
       bluetooth.startScanning({ 
        serviceUUIDs: ["133d"], 
        seconds: 4, 
        onDiscovered: (peripheral) => { 
         console.log("Periperhal found with UUID: " + peripheral.UUID); 
         this.devices.push(peripheral); // <- Problem Line 
        } 
       }).then(() => { 
        console.log("scanning complete"); 
        this.isScanning = false; 
        plusIcon.style.opacity = 1; 
       }, (err) => { 
        console.log("error while scanning: " + err); 
       }); 
       this.isScanning = false; 
      } 
     }); 
} 

をまた、Bhabishyaはのタイプを指摘したようにデバイスはObservableです。その型にはプッシュメソッドが定義されていません。代わりにのデバイスの配列を放出することができます。

配列が必要な場合は、デバイスも、Observable of arrayの配列ではなく、文字列の配列に変更する必要があります。

devices: Array<string>; 

また、初期化する必要があります。

devices: Array<string> = []; 
+0

' this'の問題は本当ですか? ** onDiscovered **関数では、 'this'はクラスを参照する必要があります。なぜなら、彼は** arows **を使用し、関数とは異なり、**はその周辺のコードと同じ字句' this'を共有します。新しい文脈はありません。 –

+0

@PaulBoutes元の投稿に矢印機能はありません。それらを追加するのが修正です。 – toskv

+1

私の悪い、私は見ていない! –

2

デバイスの定義は、配列devices: Observable<Array<string>>のObservableであり、配列devices: Array<string>ではなく、push()関数を呼び出すことができます。

関連する問題