简体   繁体   English

为什么我的 await 在我的功能中不起作用? “语法错误:等待仅在异步函数中有效”

[英]Why is my await not working in my funciton? "SyntaxError: await is only valid in async function"

I have a function -我有一个功能——

async function run() {
  const drives = await getDrives()
  drives.forEach(function (item) {
    item.size = fileSize(item.size)
    if (item.mountpoints.length > 0) {
      item.path = item.mountpoints[0].path
    } else {
      item.path = 'N/A'
    }

  })

  app.get('/', function (req, res) {
    res.render('record', {
      drives: drives
    })
  });

  app.post('/record', (req, res) => {
    const pExists = pathExists.sync(req.body.path)
    const i = req.body.url
    const test = i.lastIndexOf('/')
    const id = i.substring(test + 1)

    if (i.includes("/album/")) {
      sid = `spotify:album:${id}`
    } else if (i.includes("/playlist/")) {
      sid = `spotify:playlist:${id}`
    } else {
      console.log('failed')
    }

    if (pExists === true) {
      try {
        await myModule.mount(req.body.path, userDetails.path)
        fs.writeFileSync(`${userDetails.path}/diskplayer.contents`, sid)
        res.render('success', {
          path: req.body.path,
          url: req.body.url
        })
      } catch (err) {
        res.render('failed', {
          error: err
        })
      }
    }
  });

And on line 60 I am trying to await for myModule.mount在第 60 行,我正在尝试等待 myModule.mount

await myModule.mount(req.body.path, userDetails.path)

But I am getting the error -但我收到错误 -

await myModule.mount(req.body.path, userDetails.path)
^^^^^

SyntaxError: await is only valid in async function
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

Though my await on line 27虽然我在第 27 行等待

const drives = await getDrives()

Seems to work fine.似乎工作正常。 I can't seem to work out what I am doing wrong..我似乎无法弄清楚我做错了什么..

您需要将 async 放在这里(在您的回调中)

app.post('/record', async (req, res) => {

Always whenever you work with Async/Await remember that we can use await only if the promise generating function or task is declared as async function.每当您使用 Async/Await 时,请始终记住,只有当承诺生成函数或任务被声明为async函数时,我们才能使用await For using await, make the function in which it is used as async and making the global or parent function async has no effect.对于使用 await,将使用它的函数设为异步,并使全局或父函数异步无效。 So in your case所以在你的情况下

app.post('/record', async(req, res) => {
....
await myModule.mount(req.body.path, userDetails.path)
....
}

will work.将工作。

const drives = await getDrives()

This line is in the run() function and not inside a function in run() function, therefore it works correctly.该行位于 run() 函数中,而不是在 run() 函数中的函数内,因此它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: await 仅在我的代码中的异步 function 中有效 - SyntaxError: await is only valid in async function in my code Javascript:SyntaxError:await仅在异步函数中有效 - Javascript: SyntaxError: await is only valid in async function 未捕获的SyntaxError:await仅在异步函数的异步函数中有效 - Uncaught SyntaxError: await is only valid in async function in async function SyntaxError: await 仅在异步中有效 function **而且我有异步** - SyntaxError: await is only valid in async function **and I have async** SyntaxError: await 仅在 async 函数中有效。 无法纠正 - SyntaxError: await is only valid in async function. Unable to Correct it Appium 代码生成“SyntaxError: await is only valid in async function” - Appium code generates "SyntaxError: await is only valid in async function" Node Js - SyntaxError: await 仅在异步中有效 function - Node Js - SyntaxError: await is only valid in async function SyntaxError:“await”仅在“async”中有效 function - 需要帮助 - SyntaxError: “await” is only valid in an “async” function - Help Needed 语法错误:await 仅在异步函数 discord.js 中有效 - SyntaxError: await is only valid in async function discord.js 未捕获的 SyntaxError:await 仅在异步函数中有效(FireBase 文档) - Uncaught SyntaxError: await is only valid in async function (FireBase documentation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM