Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  40] [ 2]  / answers: 1 / hits: 37095  / 8 Years ago, sat, april 16, 2016, 12:00:00

I have the following code, to scan for a Bluetooth device, which for every device found, I want to add the device to an array.



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;
}
});
}


However, this code throws the following error:




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




I am working in Typescript, but I know the push function is a JS thing. Not sure how I would do this in Typescript - what have I done wrong?


More From » typescript

 Answers
2

It has nothing to do with TypeScript, it's just normal Javascript rules for this.



The problem this points to the function you give to onDiscovered instead of the class.



You can fix it by using the self variable you have defined or by rewriting the code to use arrow functions instead, like this:



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;
}
});
}




Also, as Bhabishya pointed out the type of devices is Observable. That type has no push method defined on it. Instead it will be able to emit an array of Devices.



If all you need is an array you should also change the devices to be an array of string, instead of an Observable of array of string.



devices: Array<string>;


You will also have to initialize it.



devices: Array<string> = [];

[#62528] Thursday, April 14, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;