运行server.js文件时出现此错误消息。错误消息:
让电影=等待响应.data.films.map(电影=> {^^^^^语法错误:等待仅在异步功能中有效使用新脚本(vm.js:79:7)在createScript(vm.js:251:10)在Object.runInThisContext(vm.js:303:10)在Module._compile(内部/模块/cjs/loader.js:657:28)在Object.Module._extensions..js(内部/模块/cjs/loader.js:700:10)在Module.load(internal / modules / cjs / loader.js:599:32)在tryModuleLoad(内部/模块/cjs/loader.js:538:12)在Function.Module._load(内部/模块/cjs/loader.js:530:3)在Function.Module.runMain(内部/模块/cjs/loader.js:742:12)在启动时(internal / bootstrap / node.js:283:19
app.get('/api/popular/movies', async (req, res, next)=> {
axiosInstance.get(`filmsNowShowing/`).then(response=> {
console.log(response.data.films)
let films = await response.data.films.map(film =>{
return {
id:film.film_id,
name:film.film_name,
textLong:film.synopsis_long,
picimage:film.images.poster
}
})
res.status(200).json(films);
}).catch(err => {
console.log(err);
});
});
您错过了async
内的.then(response => { ... })
app.get('/api/popular/movies', async (req, res, next)=> {
await axiosInstance.get(`filmsNowShowing/`).then(async response => {
console.log(response.data.films)
let films = await response.data.films.map(film =>{
return {
id:film.film_id,
name:film.film_name,
textLong:film.synopsis_long,
picimage:film.images.poster
}
})
res.status(200).json(films);
}).catch(err => {
console.log(err);
});
});