made changes to correct console errors by moving function so that var wasnt undefined but call to api still not happening