无法读取 'watchlist' 的 属性 未定义

Cannot read property of 'watchlist' undefined

 authenticate(request, response) {
    const user = userstore.getUserByEmail(request.body.email);
    if (user) {
      response.cookie('watchlist', user.email);
      logger.info('logging in' + user.email);
      response.redirect('/start');
    } else {
      response.redirect('/login');
    }
  },

 getCurrentUser (request) {
  const userEmail = request.cookies.watchlist;
  return userstore.getUserByEmail(userEmail);
  }
} 

此处的代码对用户进行身份验证,并在上方将 'watchlist' 定义为 cookie,但它一直告诉我它未定义。我该如何解决这个问题?

getUserByEmail 可能是一个异步函数。请务必使用 await 以确保我们等待 promise 解决并 return 一个值,然后再移动到下一行。在您的情况下,它将如下所示:

 async authenticate(request, response) {
    const user = await userstore.getUserByEmail(request.body.email);
    if (user) {
      response.cookie('watchlist', user.email);
      logger.info('logging in' + user.email);
      response.redirect('/start');
    } else {
      response.redirect('/login');
    }
  },

 async getCurrentUser (request) {
  const userEmail = request.cookies.watchlist;
  return await userstore.getUserByEmail(userEmail);
  }
} 

进一步阅读:JavaScript Async Await