Object is not defined in the scope - javascript

I'm trying to send information to another page, but i'm getting this error.
angular.min.js:107 ReferenceError: layoutTeste is not defined
This is my code.
vm.editar = function () {
$state.go("importacaoPreFaturaValidar", {
layoutTeste: vm.layouteste
});
}
the vm.layouteste has values, but the layoutTeste says is not defined.
What did i miss, in the scope i put.
vm.layoutTeste = [];

You need to pass the parameter within quotes
$state.go("importacaoPreFaturaValidar", {
'layoutTeste': vm.layouteste
});

I had similar error. Try:
$state.go("importacaoPreFaturaValidar", {
layoutTeste: vm.layouteste
}).bind(this)

Use arrow function instead. This way you will retain the original scope, which is now considered as undefined.
vm.editar = () => {
$state.go("importacaoPreFaturaValidar", {
layoutTeste: vm.layouteste
});
}

Related

Why method is not defined, thous it is defined?

saveEvent = (eventId, event) => {};
saveEvent(eventId, event);
What is wrong here? I think I defined clearly the method.
Use this keyword to access the class fields/properties and methods.
Example:
class SomeClass {
saveEvent = (eventId, event) => { };
someOtherFunction = () => {
this.saveEvent(eventId, event);
}
}
You missed to write this property and you are calling the saveEvent as a global function.
this.saveEvent(eventId, event)
If you're using OOP (classes) Then put the this keyword before executing the function
Like:- this.saveEvent(eventId, event). And if not, please post the complete code here but don't take a screenshot! And I'm glad to help you

TypeError: Method called on incompatible receiver undefined

My attempt to modify the standard built-in method then (see below) is throwing the following error:
TypeError: Method Promise.prototype.then called on incompatible receiver undefined
at then (<anonymous>)
at Promise.then
Implementation (Runtime: Browser)
(function() {
console.log(this.Promise);
const oldThen = this.Promise.prototype.then;
this.Promise.prototype.then = function() {
console.log('modification');
return oldThen(arguments);
};
})()
Promise.resolve(1).then(fv => {
console.log(`Promise.resolve().then(..): `, fv);
});
Any idea what is going on here?
EDIT:
Binding this to the global object via arrow functions, does not seem to work either:
(function() {
console.log(this.Promise);
const oldThen = this.Promise.prototype.then;
this.Promise.prototype.then = () => {
console.log('modification');
console.log(this.Promise); // this is now the global object
return oldThen(arguments[0]);
};
})()
Promise.resolve(1).then(fv => {
console.log(`Promise.resolve().then(..): `, fv);
});
You need to call oldThen with the correct this:
return oldThen.apply(this, arguments);
You also need to pass the arguments directly, not wrapped in an array.

Set ReactiveDict() variable inside of custom event function

I try to set a ReactiveDict-variable on a custom event inside of on(), but I get this error: Uncaught TypeError: Cannot read property 'templateDictionary' of null.
The second question is, if it would make sense to define the ReactiveDict() in onRendered?
Template.something.onCreated(function() {
this.templateDictionary = new ReactiveDict();
});
Template.something.onRendered(function() {
anything.on({
'element:mouseover': function(elementView, event){
Template.instance().templateDictionary.set( 'showExtraFields', true );
}
});
});
Template.something.helpers({
anything: function() {
var result = Template.instance().templateDictionary.get( 'showExtraFields' );
console.log(result);
}
});
Put instance reference inside onRendered function. Not inside another function. Scope issue.
I have no idea what anything.on is, but try this:
Template.something.onRendered(function() {
anything.on({
'element:mouseover': (elementView, event) => {
this.templateDictionary.set( 'showExtraFields', true );
}
});
});
Use the ES6, Luke!

Reactive variable is undefined when it is defined

I would like to get some help debugging a situation where a Reactive Variable is undefined, when it has been defined already.
This code is attaching a Reactive Variable to the template instance, and using the variable in template.autorun().
Template.home.onCreated(function () {
this.limit = new ReactiveVar(15);
this.autorun(function () {
this.subscribe('recent-topics', this.limit.get());
});
});
When I load the template for the first time, I expect the template to subscribe to recent-topics with an argument 15. However, the code throws an error:
Uncaught TypeError: Cannot read property 'get' of undefined
Any ideas why?
Just an answer for the sake of spreading the joys of ES6:
Template.home.onCreated(function () {
this.limit = new ReactiveVar(15);
this.autorun(() => {
this.subscribe('recent-topics', this.limit.get());
});
});
Make sure you add the grigio:babel package, and your Javascript file ends in .es6.js, .es6, or .jsx.
Explanation
In ES6 (aka ECMAScript 6), there's a new "fat arrow" syntax which is very similar to CoffeeScript's implementation. In ES6, when you do something like this:
someFunc = function () {
anotherThing((var1, var2) => {
this.thing = true;
});
};
It's the same as doing this:
someFunc = function () {
var self = this;
anotherThing(function (var1, var2) {
self.thing = true;
});
};
This is a scoping issue.
Inside of your Tracker.autorun, this no longer refers to the template, but the autorun's callback function. Inside of the autorun, try calling Template.instance().limit.get().
Better than using Template.instance().limit.get() (ryan's answer)
You should do something like this:
Template.home.onCreated(function () {
var self = this;
self.limit = new ReactiveVar(15);
self.autorun(function () {
self.subscribe('recent-topics', self.limit.get());
});
});

Javascript variable is undefined but it is defined

I've this method in my global controller object of my JavaScript application. Now I get the error, that the statement self.texts.buttons.disabledFinishedJobs is undefined. But I don't understand that because the console.log() statement outputs the expected value. What can be the reason?
toggleFinishedJobs: function() {
var self = this;
console.log(self.texts.buttons.disabledFinishedJobs[0]);
if (this.disabledFinished) {
$(".status_99").show();
this.disabledFinished = false;
$("btn_finishedJobs").text(self.texts.buttons.disabledFinishedJobs[0]);
} else {
$(".status_99").hide();
this.disabledFinished = true;
$("btn_finishedJobs").text(self.texts.buttons.disabledfinishedJobs[0]);
}
}
Try this:
$("btn_finishedJobs").text(self.texts.buttons.disabledFinishedJobs[0]);
^-Typo error
instead of
$("btn_finishedJobs").text(self.texts.buttons.disabledfinishedJobs[0]);
Looks like you have a typo at the end of your code. self.texts.buttons.disabledfinishedJobs instead of self.texts.buttons.disabledFinishedJobs.

Categories