codestates / codestates/HomeTownAlba
[21.11.10] 에러핸들링 (김지윤) - Delete 컨트롤러의 await 에러
- Dominant language
- JavaScript
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
## 문제 상황 및 에러 로그
- Job 테이블의 Delete 컨트롤러 구현 중 에러 발생
- async를 사용했으나, await을 사용할 수 없다고 에러 발생
- 에러메세지 : SyntaxError: await is only valid in async function
```js
module.exports = async (req, res) => { // 함수 앞에 async 기재
Job.findAll(
{where:{companyId:0}}
)
.then((data)=>{ // 아래 await 사용으로 인해 에러 발생
let jobInfo = await Job.findOne({where:{id:jobId}})
res.json({data: jobInfo})
})
}
```
## 원인 및 해결방안
- 함수 내부 콜백함수에서 await 문법을 사용할 경우, 콜백함수 앞에도 async를 기재해야 함
```js
module.exports = async (req, res) => {
Job.findAll(
{where:{companyId:0}}
)
.then(async (data)=>{ // 콜백함수 앞에도 async 붙여줘서 에러 해결
let jobInfo = await Job.findOne({where:{id:jobId}})
res.json({data: jobInfo})
})
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.