Fix undefined error in middleware.js