无法获取节点对象的对象属性

时间:2019-04-04 09:47:33

标签: javascript async-await

所以,我对节点和表达非常陌生,我遇到了类似的问题 在这里,我正在导入所有其他js文件,例如

import { User } from './user.js';

class App {
    constructor() {
        this.init();
    }

    async init() {
        this.user = await new User();
        this.team = await new Team();
        this.navbar = new Navbar();
        this.tree = new Tree();
        this.settings = await new Settings();
        this.board = await new Board();


        this.ping();
        return this;
    }

    ping() {
        //some functionality
    }
}

现在在这里创建对象

app = await new App();
console.log('app', app);

这给了我

app > App {}

点击>,我会得到这个

>user: User {username: "someusername", roles: Array(1), settings: undefined}
>navbar: Navbar {timerIsRunning: false}

我如何访问app.user之类的属性,JSON.stringify也给我空白{}

1 个答案:

答案 0 :(得分:0)

在下一个替换App类中的构造函数:

  constructor() {
        return (async () => {
            return await this.init();
        })();
    }

现在在此处创建对象

(async () => {
  const app = await new App();
  console.log("app", app);
})();