У меня есть функция -
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
})
}
}
});
И в строке 60 я пытаюсь дождаться myModule.mount
await myModule.mount(req.body.path, userDetails.path)
Но я получаю ошибку -
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
Хотя мое ожидание на линии 27
const drives = await getDrives()
Кажется, работает нормально. Я не могу понять, что я делаю неправильно ..