【发布时间】:2021-11-26 03:59:02
【问题描述】:
我正在尝试从另一个文件导入一个函数。我尝试这样做:
main.js:
const oauth2 = require('./utils/oauth2');
function startOAuth() {
var token = oauth2.OAuth2.getToken();
}
utils/oauth2.js:
module.exports = (function() {
window.OAuth2 = {
/**
* Initialize
*/
init: function() {
this._key = "token";
},
/**
* Get Token
*
* @return OAuth2 access token if it exists, null if not.
*/
getToken: function() {
try {
return window['localStorage'][this._key];
}
catch(error) {
return null;
}
}
};
OAuth2.init();
})();
我收到以下错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'getToken')
我做错了什么?为什么 NodeJS 找不到 getToken() 函数?
非常感谢您的帮助!
PS:我知道在这里说我是初学者是错误的,但我是初学者;)
【问题讨论】:
标签: javascript node.js function