简体   繁体   中英

ReferenceError: Cannot access 'loginToday' before initialization

I'm calling a function from router, that call another function,
but I get back an error:

ReferenceError: Cannot access 'loginToday' before initialization

router.get('/public', function (req, res, next) {
  Cedacri.collectData().then((resp) => { // <---- Call Controller function collectData()
    res.render('public', { data: resp });
  })
});

// DataController.js
exports.loginToday = async function loginToday() {
    // Data
    let object = {
        'total_login_bpnow': 1,
        'total_login_bpnow_desktop': 2,
        'total_login_bpnow_mobile': 3,

    }
    return object;
}

exports.collectData = async function collectData() { // <---- router call this function
    let loginToday = await loginToday(); // so here call loginToday() but here get Error: ReferenceError: Cannot access 'loginToday' before initialization
    console.log(loginToday);
}

Any idea how can I solve that?

You named your variable the same as your function and the compiler is confused:

let loginTodayResult = await loginToday();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM