Meteor.call callback
I've got the following code ...
this.graphData = {};
this.onclick = function (id){
Meteor.call('mymethod', id, function (error, result) {
if (error) {
console.log('failed', error);
} else {
console.log('success', result);
this.graphData = result;
}
});
};
But the result does not get assigned to this.graphData? What am I doing wrong?
Error is as follows..
Exception in delivering result of invoking 'mymethod': TypeError: Cannot set property 'graphData' of undefined
Many thanks in advance.
Try to assign this to variable out of callback (let's say vm = this and then use vm.graphData
I'm using...
$reactive(this).attach($scope);
..at the start of the controller code. Weirdly, all seems ok in a normal function, just not within the Meteor.call. I must be missing something obvious ..!
OK. Even odder. If I use the following variation...
this.onclick = (id) => {
Meteor.call('mymethod', id, (error, result) => {
if (error) {
console.log('failed', error);
} else {
this.graphData = result;
console.log('success', result);
}
});
...it works, but only on the second click. this.graphData does not contain the result after the first click, although the console does.
I've tried moving my code from a directive to a controller, removing 'this' and using '$scope' instead, I've tried to set 'vm = this' but from what I can tell, the Meteor.call callback does not seem to update the scope. It seems the data IS returned and assigned to the variable 'this.graphData' as the second click seems to update the $scope (assuming with the data returned from the first click!). A button that has href="" also updates the scope and the data appears. The Meteor.call method logs the data to the console correctly on click, but the scope does not reflect this.
If I go back to using $meteor.call with $scope it works as expected. eg..
this.onclick = (id) => {
$meteor.call('mymethod', id).then(function(data){
$scope.graphdata = data;
});
};
Now I see what the problem is. I was a little tired so didn't think clearly :)
Callback is not out of angular's scope.
Try to use $q promise and change this.graphData inside .then().
In my projects I have service for wrapping Meteor.call in promise, it's very helpful.
Hi, thank you for this. I struggled last night (also tired) but with fresh(ish!) mind the following works...
function exampleController($scope, $reactive, $q) {
var vm = this;
$reactive(this).attach($scope);
vm.graphdata = {};
this.onclick = (id) => {
getData(id).then(function(data){
vm.graphdata = data;
})
};
var getData = (id) => {
var deferred = $q.defer();
Meteor.call('mymethod', id, (error, result) => {
if (error) {
console.log('failed', error);
deferred.reject('error');
} else {
console.log('success', result);
deferred.resolve(result);
}
});
return deferred.promise;
};
Does this look correct? Should I change the start to this?
var vm = this;
$reactive(vm).attach($scope);
Final edit in case it helps anyone else...thanks for the advice!
function exampleController($scope, $reactive, $q) {
$reactive(this).attach($scope);
this.graphdata = {};
this.onclick = (id) => {
getData(id).then((data) => {
this.graphdata = data;
});
};
var getData = (id) => {
var deferred = $q.defer();
Meteor.call('mymethod', id, (error, result) => {
if (error) {
console.log('failed', error);
deferred.reject('error');
} else {
console.log('success', result);
deferred.resolve(result);
}
});
return deferred.promise;
};
}
after 1.3.6,it can use 'this.call' instead