DEV Community

Phaze-Mello
Phaze-Mello

Posted on

await is only valid in async functions and the top level bodies of modules

hi there. im new to coding and using Javascript. I am currently using discord.js and node.js to build a bot for my server. in my music module, i have noded it and it comes up with the error message: await is only valid in async functions and the top level bodies of modules. i have no idea how to fix this as i am new to this coding stuff. any help greatly appreciated (although you may have to go into quite a lot of detail for me to understand). the line of code that i am using that gives me this message is:

try {

const playlist = await youtube.getPlaylist(query);

      const videosObj = await playlist.getVideos(); 

      //const videos = Object.entries(videosObj);

      for (let i = 0; i < videosObj.length; i++) { 

        const video = await videosObj[i].fetch();
Enter fullscreen mode Exit fullscreen mode

i do not know how to put it into an async function.

Top comments (2)

Collapse
 
phazemello profile image
Phaze-Mello

never mind. my stepdad is going to look through the whole code and repair it

Collapse
 
toasterstrudelz profile image
toasterstrudelz

Did you find a solution? I'm having this same problem and can't seem to figure it out. Thanks in advance!