Typescript “Cannot find name ”
I am pretty new to TypeScript.
I have created a class with some private fields. When I attempt to assign a value to one of the fields in an anonymous callback function within a class method I get the error ...
(TS) Cannot Find the name '_tokens'
I suspect that there is a scoping issue but from my understanding of JavaScript this should not be a problem. I am not sure how to fix it. Any ideas?
See .. "populateTokens()" method for error.
class SingleSignOn {
private _appTokensURL: string = "/api/IFSessionCache/Auth/";
private _tokens: string;
/**
* Initialize an instance of the SingleSignOn class to manage the permissions for the
* application associated with the application.
*/
constructor() {
this.populateTokens();
};
/**
* Gets a list of permissions tokens associated with the currently logged on user for
* the application.
*/
private getApplicationTokens(): Q.IPromise<{}> {
return Unique.AJAX.Get(this._appTokensURL, null, ENUMS.AjaxContentTypes.JSON);
};
private populateTokens () {
this.getApplicationTokens().then(
function (data) {
_tokens = <string>data; // (TS) Cannot find name "_tokens"
});
};
};
javascript typescript
add a comment |
I am pretty new to TypeScript.
I have created a class with some private fields. When I attempt to assign a value to one of the fields in an anonymous callback function within a class method I get the error ...
(TS) Cannot Find the name '_tokens'
I suspect that there is a scoping issue but from my understanding of JavaScript this should not be a problem. I am not sure how to fix it. Any ideas?
See .. "populateTokens()" method for error.
class SingleSignOn {
private _appTokensURL: string = "/api/IFSessionCache/Auth/";
private _tokens: string;
/**
* Initialize an instance of the SingleSignOn class to manage the permissions for the
* application associated with the application.
*/
constructor() {
this.populateTokens();
};
/**
* Gets a list of permissions tokens associated with the currently logged on user for
* the application.
*/
private getApplicationTokens(): Q.IPromise<{}> {
return Unique.AJAX.Get(this._appTokensURL, null, ENUMS.AjaxContentTypes.JSON);
};
private populateTokens () {
this.getApplicationTokens().then(
function (data) {
_tokens = <string>data; // (TS) Cannot find name "_tokens"
});
};
};
javascript typescript
1
There are several question about this error on Stack Overflow. Do any of those help you?
– Heretic Monkey
Nov 13 '18 at 14:32
add a comment |
I am pretty new to TypeScript.
I have created a class with some private fields. When I attempt to assign a value to one of the fields in an anonymous callback function within a class method I get the error ...
(TS) Cannot Find the name '_tokens'
I suspect that there is a scoping issue but from my understanding of JavaScript this should not be a problem. I am not sure how to fix it. Any ideas?
See .. "populateTokens()" method for error.
class SingleSignOn {
private _appTokensURL: string = "/api/IFSessionCache/Auth/";
private _tokens: string;
/**
* Initialize an instance of the SingleSignOn class to manage the permissions for the
* application associated with the application.
*/
constructor() {
this.populateTokens();
};
/**
* Gets a list of permissions tokens associated with the currently logged on user for
* the application.
*/
private getApplicationTokens(): Q.IPromise<{}> {
return Unique.AJAX.Get(this._appTokensURL, null, ENUMS.AjaxContentTypes.JSON);
};
private populateTokens () {
this.getApplicationTokens().then(
function (data) {
_tokens = <string>data; // (TS) Cannot find name "_tokens"
});
};
};
javascript typescript
I am pretty new to TypeScript.
I have created a class with some private fields. When I attempt to assign a value to one of the fields in an anonymous callback function within a class method I get the error ...
(TS) Cannot Find the name '_tokens'
I suspect that there is a scoping issue but from my understanding of JavaScript this should not be a problem. I am not sure how to fix it. Any ideas?
See .. "populateTokens()" method for error.
class SingleSignOn {
private _appTokensURL: string = "/api/IFSessionCache/Auth/";
private _tokens: string;
/**
* Initialize an instance of the SingleSignOn class to manage the permissions for the
* application associated with the application.
*/
constructor() {
this.populateTokens();
};
/**
* Gets a list of permissions tokens associated with the currently logged on user for
* the application.
*/
private getApplicationTokens(): Q.IPromise<{}> {
return Unique.AJAX.Get(this._appTokensURL, null, ENUMS.AjaxContentTypes.JSON);
};
private populateTokens () {
this.getApplicationTokens().then(
function (data) {
_tokens = <string>data; // (TS) Cannot find name "_tokens"
});
};
};
javascript typescript
javascript typescript
asked Nov 13 '18 at 14:25
Gary O. StenstromGary O. Stenstrom
1,03152549
1,03152549
1
There are several question about this error on Stack Overflow. Do any of those help you?
– Heretic Monkey
Nov 13 '18 at 14:32
add a comment |
1
There are several question about this error on Stack Overflow. Do any of those help you?
– Heretic Monkey
Nov 13 '18 at 14:32
1
1
There are several question about this error on Stack Overflow. Do any of those help you?
– Heretic Monkey
Nov 13 '18 at 14:32
There are several question about this error on Stack Overflow. Do any of those help you?
– Heretic Monkey
Nov 13 '18 at 14:32
add a comment |
3 Answers
3
active
oldest
votes
You are using the wrong syntax:
this.getApplicationTokens().then(
(data) => {
this._tokens = <string>data; // note: turned into an arrow function and added the `this` keyword
});
note if you kept using function() ...
syntax the this
keyword will not point to the class instance but to the callee
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
greetings
add a comment |
Properties of a class do not have a scope, they live as long as the object they belong to live and everything that can access the object can access all its properties too. However properties always have to be accessed on their object, e.g. something._tokens
or this._tokens
inside methods. Also you have to make sure that this
is what you think it is, in your case you have to use an arrow function to access the correct this
inside a callback:
this.getApplicationTokens().then( (data) => {
this._tokens = data as string;
});
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
add a comment |
I think you're just missing the this
keyword from _tokens
:
this._tokens = <string>data;
this is not enough, the OP also has to change the callback function into an arrow function, otherwisethis
will not point to the classes instance
– messerbill
Nov 13 '18 at 14:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283191%2ftypescript-cannot-find-name-fieldname%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are using the wrong syntax:
this.getApplicationTokens().then(
(data) => {
this._tokens = <string>data; // note: turned into an arrow function and added the `this` keyword
});
note if you kept using function() ...
syntax the this
keyword will not point to the class instance but to the callee
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
greetings
add a comment |
You are using the wrong syntax:
this.getApplicationTokens().then(
(data) => {
this._tokens = <string>data; // note: turned into an arrow function and added the `this` keyword
});
note if you kept using function() ...
syntax the this
keyword will not point to the class instance but to the callee
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
greetings
add a comment |
You are using the wrong syntax:
this.getApplicationTokens().then(
(data) => {
this._tokens = <string>data; // note: turned into an arrow function and added the `this` keyword
});
note if you kept using function() ...
syntax the this
keyword will not point to the class instance but to the callee
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
greetings
You are using the wrong syntax:
this.getApplicationTokens().then(
(data) => {
this._tokens = <string>data; // note: turned into an arrow function and added the `this` keyword
});
note if you kept using function() ...
syntax the this
keyword will not point to the class instance but to the callee
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
greetings
answered Nov 13 '18 at 14:30
messerbillmesserbill
2,9411125
2,9411125
add a comment |
add a comment |
Properties of a class do not have a scope, they live as long as the object they belong to live and everything that can access the object can access all its properties too. However properties always have to be accessed on their object, e.g. something._tokens
or this._tokens
inside methods. Also you have to make sure that this
is what you think it is, in your case you have to use an arrow function to access the correct this
inside a callback:
this.getApplicationTokens().then( (data) => {
this._tokens = data as string;
});
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
add a comment |
Properties of a class do not have a scope, they live as long as the object they belong to live and everything that can access the object can access all its properties too. However properties always have to be accessed on their object, e.g. something._tokens
or this._tokens
inside methods. Also you have to make sure that this
is what you think it is, in your case you have to use an arrow function to access the correct this
inside a callback:
this.getApplicationTokens().then( (data) => {
this._tokens = data as string;
});
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
add a comment |
Properties of a class do not have a scope, they live as long as the object they belong to live and everything that can access the object can access all its properties too. However properties always have to be accessed on their object, e.g. something._tokens
or this._tokens
inside methods. Also you have to make sure that this
is what you think it is, in your case you have to use an arrow function to access the correct this
inside a callback:
this.getApplicationTokens().then( (data) => {
this._tokens = data as string;
});
Properties of a class do not have a scope, they live as long as the object they belong to live and everything that can access the object can access all its properties too. However properties always have to be accessed on their object, e.g. something._tokens
or this._tokens
inside methods. Also you have to make sure that this
is what you think it is, in your case you have to use an arrow function to access the correct this
inside a callback:
this.getApplicationTokens().then( (data) => {
this._tokens = data as string;
});
answered Nov 13 '18 at 14:31
Jonas WilmsJonas Wilms
56.1k42851
56.1k42851
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
add a comment |
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
Thanks. I have not learned much/anything about arrow functions! Also for the helpful link!
– Gary O. Stenstrom
Nov 13 '18 at 14:39
add a comment |
I think you're just missing the this
keyword from _tokens
:
this._tokens = <string>data;
this is not enough, the OP also has to change the callback function into an arrow function, otherwisethis
will not point to the classes instance
– messerbill
Nov 13 '18 at 14:35
add a comment |
I think you're just missing the this
keyword from _tokens
:
this._tokens = <string>data;
this is not enough, the OP also has to change the callback function into an arrow function, otherwisethis
will not point to the classes instance
– messerbill
Nov 13 '18 at 14:35
add a comment |
I think you're just missing the this
keyword from _tokens
:
this._tokens = <string>data;
I think you're just missing the this
keyword from _tokens
:
this._tokens = <string>data;
edited Nov 13 '18 at 14:34
qiAlex
2,0261724
2,0261724
answered Nov 13 '18 at 14:30
KCM78KCM78
34
34
this is not enough, the OP also has to change the callback function into an arrow function, otherwisethis
will not point to the classes instance
– messerbill
Nov 13 '18 at 14:35
add a comment |
this is not enough, the OP also has to change the callback function into an arrow function, otherwisethis
will not point to the classes instance
– messerbill
Nov 13 '18 at 14:35
this is not enough, the OP also has to change the callback function into an arrow function, otherwise
this
will not point to the classes instance– messerbill
Nov 13 '18 at 14:35
this is not enough, the OP also has to change the callback function into an arrow function, otherwise
this
will not point to the classes instance– messerbill
Nov 13 '18 at 14:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283191%2ftypescript-cannot-find-name-fieldname%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There are several question about this error on Stack Overflow. Do any of those help you?
– Heretic Monkey
Nov 13 '18 at 14:32